简体   繁体   English

使用Java从文件读取数据

[英]Read data from file using Java

I have file where every line represents vertice. 我有每行代表顶点的文件。 (format for example- 1.0 0.0 vertice A) My task is to create method (例如格式-1.0 0.0顶点A)我的任务是创建方法

public void read(InputStream is) throws IOException

Which would save X and Y values of vertices and then label of it "vertice A". 这将保存顶点的X和Y值,然后将其标签为“顶点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? 您能告诉我这两种方法之间的区别吗?

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. System.In是未提供文件的输入流的示例,它是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
}

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 :). 您也可以通过使用StringTokenizer避免使用split,这是另一种方法:)。

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). 如另一个答案所述,文件仅是文件系统中节点的表示,它仅指向文件系统中现有的元素,但此时内部文件中不包含任何数据或信息,我的意思是,仅作为文件提供信息(如果是文件,目录或类似文件)(如果不存在,则您将收到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. InputStream是一个数据序列,此时,您应该在此处具有需要由BufferedReader,ObjectInputStream或其他组件处理或读取的信息,具体取决于您需要执行的操作。

For more info, you can also ask to your friendly API docs: 有关更多信息,您还可以询问友好的API文档:

https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html

https://docs.oracle.com/javase/7/docs/api/java/io/File.html https://docs.oracle.com/javase/7/docs/api/java/io/File.html

Regards and... happy coding :) 问候和...快乐编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM