導航:首頁 > 文件類型 > javalinux獲取文件

javalinux獲取文件

發布時間:2022-12-23 16:43:53

linux下使用java讀取文件,路徑格式問題!

linux文件系統也有絕對路徑,從根目錄開始的。
當然你也可以讀相對路徑,./表示當前路徑,..表示上一級路徑。

⑵ 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;
}

⑶ java程序怎樣讀取linux系統下的文件

importjava.io.*;

publicclassFileToString{
publicstaticStringreadFile(StringfileName){
Stringoutput="";

Filefile=newFile(fileName);

if(file.exists()){
if(file.isFile()){
try{
BufferedReaderinput=newBufferedReader(newFileReader(file));
StringBufferbuffer=newStringBuffer();
Stringtext;

while((text=input.readLine())!=null)
buffer.append(text+"/n");

output=buffer.toString();
}
catch(IOExceptionioException){
System.err.println("FileError!");

}
}
elseif(file.isDirectory()){
String[]dir=file.list();
output+="Directorycontents:/n";

for(inti=0;i<dir.length;i++){
output+=dir[i]+"/n";
}
}
}
else{
System.err.println("Doesnotexist!");
}
returnoutput;
}

publicstaticvoidmain(Stringargs[]){
Stringstr=readFile("/home/1.txt");

System.out.print(str);
}
}

⑷ 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通過SSH獲取Linux文件出錯

1
ssh
在cygwin中執行:$
ssh
username@remotehost
2
scp
命令scp基於ssh協議,可以將本地文件拷貝到遠程服務上的指定目錄

⑹ java在Linux獲取EXE文件圖標

這個沒實驗過,你自己體會細細品。。。

⑺ linux 下java讀取配置文件

linux下也是文件系統,同樣的也可以使用file對象來讀取配置文件信息,示例如下:
import java.io.*;

public class FileToString {
public static String readFile(String fileName) {
String output = "";

File file = new File(fileName);//建立file對象

if(file.exists()){//判斷是否存在
if(file.isFile()){//判斷是否文件
try{
BufferedReader input = new BufferedReader (new FileReader(file));
StringBuffer buffer = new StringBuffer();
String text;

while((text = input.readLine()) != null)
buffer.append(text +"/n");//讀取內容進行拼接。

output = buffer.toString();
}
catch(IOException ioException){
System.err.println("File Error!");

}
}
else if(file.isDirectory()){//是否為文件夾
String[] dir = file.list();
output += "Directory contents:/n";

for(int i=0; i<dir.length; i++){
output += dir[i] +"/n";
}
}
}
else{
System.err.println("Does not exist!");
}
return output;
}
}

⑻ java怎麼取出linux伺服器的文件路徑

java可以獲取當前項目的路徑, Class.class.getClass().getResource("/").getPath()
這個是有請求回的情況下答,request.getSession().getServletContext().getRealPath("")

⑼ 如何用java獲取linux下某文件夾的大小

通過FileInputStream來獲取的文件大小:
public static void main(String[] args) {
FileInputStream fis= null;
try{
File f= new File("D:\\CentOS-6.5-x86_64-bin-DVD1.iso");
fis= new FileInputStream(f);
logger.info(fis.available());
}catch(Exception e){
logger.error(e);
} finally{
if (null!=fis){
try {
fis.close();
} catch (IOException e) {
logger.error(e);
}
}
}
}
下面是運行結果:
2147483647
它是Integer.MAX_VALUE,也就是有符號整型能表示的最大數值。

閱讀全文

與javalinux獲取文件相關的資料

熱點內容
蘋果第三方打開文件 瀏覽:310
蘋果電腦桌面移除的文件夾怎麼恢復 瀏覽:292
在文件名前添加字元 瀏覽:424
javaexcel導出控制項 瀏覽:484
怎麼樣粉碎文件 瀏覽:798
谷歌網站怎麼更新 瀏覽:934
svn命令行刪除文件夾 瀏覽:277
如何建立共享數據表 瀏覽:247
ps存儲覆蓋了源文件 瀏覽:668
如何找出合計數的數據 瀏覽:475
筆記本doc文件在哪裡 瀏覽:206
文件類的東西包括哪些 瀏覽:442
js初始化載入圖片 瀏覽:616
哪些網站可以下文件 瀏覽:910
米64g網路怎麼開 瀏覽:502
2017互聯網java面試題 瀏覽:907
供求網站源碼 瀏覽:195
童程童美編程加盟怎麼樣 瀏覽:895
app美團如何下載 瀏覽:197
弄畫框用什麼app 瀏覽:814

友情鏈接