Reputation: 151
I have file where every line represents vertice. (format for example- 1.0 0.0 vertice A) My task is to create method
public void read(InputStream is) throws IOException
Which would save X and Y values of vertices and then label of it "vertice A". I have no idea how to parse it properly:
public void read(InputStream is) throws IOException {
try {
Reader r = new InputStreamReader(is);
BufferedReader br = new BufferedReader(r);
while(br.readLine()!=null){
//something
}
} catch(IOException ex){
ex.printStackTrace();
}
}
also I need to create method
public void read(File file) throws IOException
which makes exactly the same but with file instead of stream. Can you tell me difference between these two methods?
Upvotes: 1
Views: 87
Reputation: 3856
A File represents a node on the filesystem, a stream is a representation of a sequence of data with a read head. Opening a file for reading results in an input stream. System.In is an example of an input stream for which you did not provide a file, it is the stream for stdin.
public void read(File file) throws IOException
{
//using your input stream method, read the passed file
//Create an input stream from the given file, and then call what you've already implemented.
read(new FileInputStream(file));
//I assume your read function closes the stream when it's done
}
Upvotes: 2
Reputation: 3881
I'd do the following and explain it through the code :)
public void read(InputStream is) throws IOException {
//You create a reader hold the input stream (sequence of data)
//You create a BufferedReader which will wrap the input stream and give you methods to read your information
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
handleVerticeValues(reader);
reader.close();
}
public void read(File file) throws IOException {
//You create a buffered reader to manipulate the data obtained from the file representation
BufferedReader reader = new BufferedReader(new FileReader(file));
handleVerticeValues(reader);
reader.close();
}
private void handleVerticeValues(BufferedReader reader) throws IOException {
//Then you can read your file like this:
String lineCursor = null;//Will hold the value of the line being read
//Assuming your line has this format: 1.0 0.0 verticeA
//Your separator between values is a whitespace character
while ((lineCursor = reader.readLine()) != null) {
String[] lineElements = lineCursor.split(" ");//I use split and indicates that we will separate each element of your line based on a whitespace
double valX = Double.parseDouble(lineElements[0]);//You get the first element before an whitespace: 1.0
double valY = Double.parseDouble(lineElements[1]);//You get the second element before and after an whitespace: 0.0
String label = lineElements[2];//You get the third element after the last whitespace
//You do something with your data
}
}
You can avoid using split by using StringTokenizer as well, that is another approach :).
As mentioned in the other answer, a file is only a representation of a node in your file system, it just point to an element existing in your file system but it not hold any data or information at this point of your internal file, I mean, just information as a file (if is a file, directory or something like that) (if it does not exist, you receive a FileNotFoundException).
The InputStream is a sequence of data, at this point, you should have information here which needs to be handled or read by a BufferedReader, ObjectInputStream or another component, depending on what you need to do.
For more info, you can also ask to your friendly API docs:
https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html
https://docs.oracle.com/javase/7/docs/api/java/io/File.html
Regards and... happy coding :)
Upvotes: 0