『壹』 java程序中怎麼獲取linxu系統的根目錄
根目錄的路徑就是/,永遠不變,直接寫/即可,無需獲取。
『貳』 linux下 Java如何獲取文件的絕對路徑
需要使用路徑時,用下面的方法取得項目根目錄的絕對路徑(Tools為方法類)
public static String getRootPath() {
String classPath = Tools.class.getClassLoader().getResource("/").getPath();
String rootPath = "";
//windows下
if("\\".equals(File.separator)){
rootPath = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("/", "\\");
}
//linux下
if("/".equals(File.separator)){
rootPath = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("\\", "/");
}
return rootPath;
}
『叄』 linux下java讀取文件路徑怎麼寫
linux下文件路徑都是「/」開始的,可以通過changeWorkingDirectory方法來進行路徑的切換,舉例:
**
* 上傳文件
*
* @param fileName
* @param plainFilePath 文件路徑路徑
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上傳文件開始");
Log.info("連接遠程上傳伺服器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("檢查文件路徑是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查詢文件路徑不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 設置文件類型(二進制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上傳文件成功:"+fileName+"。文件保存路徑:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
備註:只需要修改上傳的伺服器地址、用戶名、密碼即可進行伺服器訪問上傳。根據實際需要修改即可。
『肆』 java怎麼取出linux伺服器的文件路徑
java可以獲取當前項目的路徑, Class.class.getClass().getResource("/").getPath()
這個是有請求的情況下,request.getSession().getServletContext().getRealPath("")
『伍』 用java如何讀取linux中的某個文件
java是跨平台語言,在linux上讀文件跟在windows上讀文件是一樣的 只是文件路徑不一樣,可以用File對象和FileInputSteam來讀取。但要注意文件編碼問題。
如果有中文請做適當的編碼轉換,通常情況下Linux的默認字元編碼為UTF-8編碼方式,項目可以直接採用utf8編碼方式操作.用System.getProperty("file.encoding")可檢查系統編碼格式。可改操作系統的文件系統編碼,vi /etc/profile,在文件末尾加上
export LANG="zh_CN.GBK"
export LC_ALL="zh_CN.GBK"
編碼轉換代碼:new String(files[i].getName().getBytes("GBK"),"UTF-8");
文件操作的核心代碼請參考下面代碼:
String path= "/home/";
path= "/home/multiverse/Repository/PMEPGImport";
File file=new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
//FileInputStream fis = new FileInputStream("fileName");
//InputStreamReader isr = new InputStreamReader(fis,"utf-8");
StringBuffer buffer = new StringBuffer();
String text;
BufferedReader input = new BufferedReader (new FileReader(tempList[i]));
while((text = input.readLine()) != null)
buffer.append(text +"/n"); }
if (tempList[i].isDirectory()) {
System.out.println("文件夾:"+tempList[i]);
}
}
『陸』 java如何拼接linux目錄下文件路徑
java在讀取linux目錄時可以使用FileSystem類,FileSystem創建IO流時需要Path子類,新建Path只需要傳入String類型的路徑即可。
所以拼接路徑實際上就是對String的拼接。
String有多種方法可以拼接,最簡單的是直接用+號來接。
『柒』 java獲取linux路徑怎麼寫
liunx 沒有window中的盤符 只有一個根目錄 不能用「\\」 會被轉義 只能用「/」寫 你用pwd命令查詢 文件跟路徑 然後拼文件全名 應該就可以的。。 試試
『捌』 java如何獲得linux下web路徑
java獲取根路徑有兩種方式:
1),在servlet可以用一下方法取得:
request.getRealPath(「/」) 例如:filepach = request.getRealPath(「/」) 」//upload//」;
2),不從jsp,或servlet中獲取,只從普通java類中獲取:
String path =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
SAXReader() saxReader = new SAXReader();
if(path.indexOf(「WEB-INF」)>0){
path = path.substring(0,path.indexOf(「/WEB-INF/classes」) 16);
// 『/WEB-INF/classes』為16位
document = saxReader.read(path filename);
}else{
document = saxReader.read(getClass().getResourceAsStream(filename));
}
weblogic tomcat 下都有效
String path =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
<!--EndFragment-->
『玖』 java linux怎麼獲取文件路徑
一般文件路徑在windows中用 \ 表示,但是在其他系統平台下比如linux中就不是 \ 所以java給我們提供了一個與平台無關的表示路徑的常量 File.separator在windows中則表示 \ 比如現在有一個文件在D:\java\src\myjava中, 如何用絕對路徑訪問呢?
現在建立一個目錄:
File fDir=new File(File.separator); //File.separator表示根目錄,比如現在就表示在D盤下。
String strFile="java"+File.separator+"src"+File.separator+"myjava"; //這個就是絕對路徑
File f=new File(fDir,strFile);
『拾』 用java如何讀取linux中的某個文件
java是跨平台語言,在linux上讀文件跟在windows上讀文件是一樣的 只是文件路徑不一樣,可以用File對象和FileInputSteam來讀取。但要注意文件編碼問題。
如果有中文請做適當的編碼轉換,通常情況下Linux的默認字元編碼為UTF-8編碼方式,項目可以直接採用utf8編碼方式操作.用System.getProperty("file.encoding")可檢查系統編碼格式。可改操作系統的文件系統編碼,vi /etc/profile,在文件末尾加上
export LANG="zh_CN.GBK"
export LC_ALL="zh_CN.GBK"
編碼轉換代碼:new String(files[i].getName().getBytes("GBK"),"UTF-8");
文件操作的核心代碼請參考下面代碼:
String path= "/home/";
path= "/home/multiverse/Repository/PMEPGImport";
File file=new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
//FileInputStream fis = new FileInputStream("fileName");
//InputStreamReader isr = new InputStreamReader(fis,"utf-8");
StringBuffer buffer = new StringBuffer();
String text;
BufferedReader input = new BufferedReader (new FileReader(tempList[i]));
while((text = input.readLine()) != null)
buffer.append(text +"/n"); }
if (tempList[i].isDirectory()) {
System.out.println("文件夾:"+tempList[i]);
}
}