linuxjava读取文件内容|用java如何读取linux中的某个文件

linuxjava读取文件内容|用java如何读取linux中的某个文件的第1张示图

A. 用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]); } }

B. 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; }}

C. linux下,从如何文件路径字符串中java提取文件名。

我是写C的,懂一点linux。C语言里面有readdir库函数,可以读文件,然后会保存在回d_name结构体里,直接用答就行。Java的话,通过ls >文件的命令,把文件列表存文件里再读取,是不是一个好办法?

D. 怎样用 java读取txt文件中的数据 linux下

try {BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("文件路径")));for (String line = br.readLine(); line !版= null; line = br.readLine()) {System.out.println(Integer.parseInt(line));}}catch (FileNotFoundException e) {权 e.printStackTrace();}catch (IOException e) { e.printStackTrace();}

E. 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]);}}

F. java 读取linux文件内容乱码 但是在linux上查看文件是正常

选用正确的charset

G. linux下,编写java程序,读取另一台linux下的文件内容,路径格式怎么写,有没有方法实现

这要应用到linux的文来件共享自机制,也就是说你的程序要依赖linux系统的设置。简单来说,就是要访问另一台linux下的文件,你的linux系统必须能访问到那个文件,通过文件共享,然后mount到本地就可以了。

未经允许不得转载:山九号 » linuxjava读取文件内容|用java如何读取linux中的某个文件

赞 (0)