Ⅰ java下linux路徑怎麼寫
不也和其他一樣么?不過為了不出現歧義,建議用System.getProperty()方法生成路徑。 比如斜杠這樣寫:System.getProperty("file.separator") 。
Ⅱ 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-->
Ⅳ 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伺服器端「/「文件路徑如何書寫
樓主可以這樣寫【File.separator是java虛擬機根據當前的操作系統自動識別得到的文件路徑分隔符,例如windows是「」,linux是」/「】:
Filefile=newFile("files"+File.separator+"temp"+File.separator+"test.txt");
Ⅶ 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怎麼獲取文件路徑
一般文件路徑在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獲取某個文件夾的路徑怎麼寫
File類有兩個常用方法可以得到文件路徑一個是:getCanonicalPath(),另一個是:getAbsolutePath(),可以通過File類的實例調用這兩個方法例如file.getAbsolutePath()其中file是File的實例對象。下面是一個具體例子:
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之處在於,getCanonicalPath()得到的是一個規范的路徑,而getAbsolutePath()是用構造File對象的路徑+當前工作目錄。例如在上面的例子中.(點號)代表當前目錄。getCanonicalPath()就會把它解析為當前目錄但是getAbsolutePath()會把它解析成為目錄名字(目錄名字是點號)。
下面是上面程序在我電腦上的輸出:
G:\xhuoj\konw\.\src\
G:\xhuoj\konw\src\
Ⅹ java程序中怎麼獲取linxu系統的根目錄
根目錄的路徑就是/,永遠不變,直接寫/即可,無需獲取。