2
  • 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:

enter image description here

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

Community
  • 1
  • 1
Jay
  • 59
  • 1
  • 7
  • Looks like i just needed to use throws IOException {} to cancel the exceptions, now only question is: How do I correctly grab my .dat file.. I'm not getting this error: Exception in thread "main" java.io.FileNotFoundException: dichromatic.dat (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.io.FileReader.(Unknown Source) at frp.FileRead.main(FileRead.java:11) – Jay Feb 27 '14 at 16:13
  • OK, actually I brought my .dat file out of src and dropped it into frp project folder, and console printed null, now I will try different data reading methods of java.io... – Jay Feb 27 '14 at 16:15
  • 1
    I think the bigger question is, what format is this ".dat" file? Is it a binary file format? If so, what is the layout? Depending on the answer, the best way to deal with this file will change. – ProgrammerDan Feb 27 '14 at 16:35
  • btw, what is a *control point* ? – rpax Feb 27 '14 at 17:57
  • I've been googling around looking for .dat files containing *control points*, but the only standard format (I didn't knew that exists an standard format of .dat files) I've found is either: *Cambridge .DAT* file format or a type of csv containing waypoints. In what format is your "dichromatic.dat" file ? could you post a sample? – rpax Feb 27 '14 at 18:24
  • 1
    Sorry for confusion, we refer to x and y points grouped together as control points for forming a graph, because they control what the graph will look like. So, I was referring to the points as such, but really I'm just looking to get the x and y values from an array, and actually I found the source code that's reading and storing the .dat file info into an array.. so this entire question is now misplaced for my current application.. – Jay Feb 27 '14 at 18:34
  • @Jay But you will need more than x,y for forming a graph,don't you? How do you connect the points? – rpax Feb 27 '14 at 18:36
  • Of course I will be drawing curves on a canvas 2d context, but I need to know the points from the .dat file so I know where I'm drawing to.. The new question is here: http://stackoverflow.com/questions/22078454/pulling-this-custom-readdatafile-function-into-eclipse-to-print-dat-file-data-t – Jay Feb 27 '14 at 19:30

2 Answers2

3

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();
    }
rpax
  • 4,468
  • 7
  • 33
  • 57
  • 1
    Nicely done rpax, that's definitely a more mature application of Exception catching. @Jay -- this is a good example of what I was mentioning, which is catching exceptions for the smallest unit possible. – ProgrammerDan Feb 27 '14 at 16:27
  • @ProgrammerDan That's right, but it makes the code uglier. It's better, but makes it more complicated to read. – rpax Feb 27 '14 at 16:54
2

I would strongly recommend spending some time reading through the Java Trails Tutorials. To answer your specific question, look at Lesson: Exceptions.

To oversimplify, just wrap the file-handling code in a try...catch block. By example:

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

Note that ideally, your try...catch should wrap the smallest possible unit of code. So, wrap the FileReader separately, and "fail-fast" if the file isn't found, and wrap the readLine loop in its own try...catch. For more examples and a better explanation of how to deal with exceptions, please reference the link I provided at the top of this answer.

Edit: issue of file path

Not finding the file has to do with the location of the file relative to the root of the project. In your original post, you reference the file as "dichromatic.dat" but relative to the project root, it is in "src/frp/dichromatic.dat". As rpax recommends, either change the string that points to the file to properly reference the location of the file relative to the project root, or move the file to project root and leave the string as-is.

ProgrammerDan
  • 871
  • 7
  • 17