在eclipse中使用java.io库,因此FileInputStream可以读取一个dat文件 [英] Using java.io library in eclipse so FileInputStream can read a dat file

查看:219
本文介绍了在eclipse中使用java.io库,因此FileInputStream可以读取一个dat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 目标:使用Eclipse将数据从.dat文件打印到控制台。


    • (长期目标):可执行文件,我可以传递一个.dat文件,并创建一个新的txt文件数据格式化。




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



Eclipse安装程序:




  • 创建Java项目


  • 新建>类..称为类 FileRead

    li>


现在我有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 (新文件(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 /}

请注意,这种方式是从这里借来的: LINES 12,14,17



QUESTION :如何防止这些异常?



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

解决方案

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



2. 执行以下操作:

  public static void main(String [] args){
FileReader file =空值;
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);
字符串;
尝试{
while((line = br.readLine())!= null){
System.out.println(line);
}

} catch(IOException e){
System.err.println(读取时出错);
e.printStackTrace();
} finally {
if(br!= null){
try {
br.close();
} catch(IOException e){
System.err.println(Unexpected error);
e.printStackTrace();
}
}
}
}

strong> 3。创建一个新的txt文件格式化。在这个例子中,格式化将字符设置为大写。

  public static void main(String [] args){
FileReader file = null;
BufferedWriter bw = null;
文件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 not a file);
e1.printStackTrace();
}
BufferedReader br = new BufferedReader(file);
字符串;
字符串线形格式;
尝试{
while((line = br.readLine())!= null){
lineformatted = format(line);
bw.write(lineformatted);
//如果你需要它
bw.newLine();
}

} catch(IOException e){
System.err.println(处理文件时出错!);
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){
//将此替换为您的需要
return line.toUpperCase();
}


  • Goal: Print the data from a .dat file to the console using Eclipse.
    • (Long-Term Goal): Executable that I can pass a .dat file to and it creates a new txt file with the data formatted.

The .dat: I know the .dat file contains control points that I will need to create a graph with using ECMAScript.

Eclipse Setup:

  • Created Java Project

  • New > Class .. called the Class FileRead

Now I have FileRead.java which is:

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/   }

Please note this approach was borrowed from here: https://stackoverflow.com/a/18979213/3306651

1st Challenge: FileNotFoundException on LINE 10

Screenshot of Project Explorer:

QUESTION: How to correctly reference the .dat file?

2nd Challenge: Unhandled exception type IOException LINES 12, 14, 17

QUESTION: How to prevent these exceptions?

Thank you for your time and effort to help me, I am recreating Java applets using only JavaScript. So, I'm looking to create java tools that extract data I need to increase productivity. If you are interested in phone/web app projects involving JavaScript, feel free to contact me 8503962891

解决方案

1. Without changing your code, you must place the file in the project's root folder. Otherwise, reference it as src/frp/dichromatic.dat

2. Doing something like this:

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. Creation of a new txt file "formatted". In this example, the formatting will be settings the characters to uppercase.

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();
    }

这篇关于在eclipse中使用java.io库,因此FileInputStream可以读取一个dat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆