在eclipse中使用java.io库,因此FileInputStream可以读取dat文件

  • 目标 :使用Eclipse将数据从.dat文件打印到控制台。
    • (长期目标) :我可以将.dat文件传递给的可执行文件,它会创建一个格式化数据的新txt文件。

.dat :我知道.dat文件包含使用ECMAScript创建图形所需的控制点。

Eclipse安装程序:

  • 创建Java项目

  • New> Class ..称为类FileRead

现在我有FileRead.java,它是:

1/ package frp; 2/ 3/ import java.io.BufferedReader; 4/ import java.io.File; 5/ import java.io.FileReader; 6/ 7/ public class FileRead { 8/ 9/ public static void main(String[] args) { 10/ FileReader file = new FileReader(new File("dichromatic.dat")); 11/ BufferedReader br = new BufferedReader(file); 12/ String temp = br.readLine(); 13/ while (temp != null) { 14/ temp = br.readLine(); 15/ System.out.println(temp); 16/ } 17/ file.close(); 18/ } 19/ 20/ } 

请注意这种方法是从这里借来的: https : //stackoverflow.com/a/18979213/3306651

第一个挑战:第 10行的FileNotFoundException

Project Explorer的屏幕截图:

在此处输入图像描述

问题 :如何正确引用.dat文件?

第二个挑战:未处理的exception类型IOException LINES 12,14,17

问题 :如何防止这些exception?

感谢您花时间和精力帮助我,我正在使用JavaScript重新创建Java小程序。 所以,我正在寻找创建提取生产力所需数据的java工具。 如果您对涉及JavaScript的电话/网络应用程序项目感兴趣,请随时与我联系8503962891

1.在不更改代码的情况下,必须将文件放在项目的根文件夹中。 否则,将其引用为src/frp/dichromatic.dat

2.做这样的事情:

 public static void main(String[] args) { FileReader file = null; try { file = new FileReader(new File("dichromatic.dat")); } catch (FileNotFoundException e1) { System.err.println("File dichromatic.dat not found!"); e1.printStackTrace(); } BufferedReader br = new BufferedReader(file); String line; try { while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.err.println("Error when reading"); e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { System.err.println("Unexpected error"); e.printStackTrace(); } } } } 

3.创建“格式化”的新txt文件。 在此示例中,格式化将字符设置为大写。

 public static void main(String[] args) { FileReader file = null; BufferedWriter bw = null; File outputFile = new File("output.formatted"); try { file = new FileReader(new File("dichromatic.dat")); } catch (FileNotFoundException e1) { System.err.println("File dichromatic.dat not found!"); e1.printStackTrace(); } try { bw = new BufferedWriter(new FileWriter(outputFile)); } catch (IOException e1) { System.err.println("File is not writtable or is not a file"); e1.printStackTrace(); } BufferedReader br = new BufferedReader(file); String line; String lineformatted; try { while ((line = br.readLine()) != null) { lineformatted = format(line); bw.write(lineformatted); // if you need it bw.newLine(); } } catch (IOException e) { System.err.println("Error when processing the file!"); e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { System.err.println("Unexpected error"); e.printStackTrace(); } } if (bw != null) { try { bw.close(); } catch (IOException e) { System.err.println("Unexpected error"); e.printStackTrace(); } } } } public static String format(String line) { // replace this with your needs return line.toUpperCase(); } 

我强烈建议花一些时间阅读Java Trails Tutorials。 要回答您的具体问题,请查看课程:例外 。

要过度简化,只需将文件处理代码包装在try...catch块中。 例如:

 package frp; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public class FileRead { public static void main(String[] args) { try { FileReader file = new FileReader(new File("dichromatic.dat")); BufferedReader br = new BufferedReader(file); String temp = br.readLine(); while (temp != null) { temp = br.readLine(); System.out.println(temp); } file.close(); } catch (FileNotFoundException fnfe) { System.err.println("File not found: " + fnfe.getMessage() ); } catch (IOException ioe) { System.err.println("General IO Error encountered while processing file: " + ioe.getMessage() ); } } } 

请注意,理想情况下, try...catch应该包含尽可能小的代码单元。 因此,单独包装FileReader ,如果找不到文件则“fail-fast”,并将readLine循环包装在自己的try...catch 。 有关更多示例以及如何处理exception的更好解释,请参阅我在本答案顶部提供的链接。

编辑:文件路径的问题

找不到文件与文件相对于项目根目录的位置有关。 在原始post中,您将文件引用为“dichromatic.dat”,但相对于项目根目录,它位于“src / frp / dichromatic.dat”中。 正如rpax建议的那样,更改指向文件的字符串以正确引用文件相对于项目根目录的位置,或者将文件移动到项目根目录并保持字符串不变。