javacode如下:import java.io.*; class dirlen { static long dirlength;//保存目錄大小的變數 static void sdl(String dirname) { File dir=new File(dirname); System.out.println(dirname); String f[]=dir.list(); File f1; for(int i=0;i<f.length;i++) { f1 = new File (dirname+"/"+f[i]); if (!f1.isDirectory()) dirlength+=f1.length(); else sdl(dirname+"/"+f[i]);//如果是目錄,遞歸調用 } } public static void main(String args[]) { if(args.length!=1)//判斷是否只帶一個參數,參數是目錄名 { System.out.println("Parameter error!"); System.exit(0); } dirlength=0; String dirname=args[0]; File dir=new File(dirname); if(dir.isDirectory())//判斷是否是目錄,如果不是退出程序 { sdl(dirname);//計算目錄大小 System.out.println("Length is "+dirlength+" bytes."); } else System.out.println(dir+" isn't a directory!"); } }
㈡ 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下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中的某個文件
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]);
}
}
㈤ 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怎麼獲取文件路徑
一般文件路徑在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);
㈦ linux 下java讀取配置文件
如果是相對路徑無論windows還是linux都是一樣的。
如果是絕對路徑,只需要看好配置文件所在linux的目錄,其他的操作和windows一樣(例如:linux【/usr/local/src/1.config】,windows【c:/1.config】)
使用File就可以直接讀取了。
㈧ java怎麼取出linux伺服器的文件路徑
java可以獲取當前項目的路徑, Class.class.getClass().getResource("/").getPath()
這個是有請求的情況下,request.getSession().getServletContext().getRealPath("")
㈨ 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;
}