繁体   English   中英

使用Java从文件读取数据

[英]Read data from file using Java

我有每行代表顶点的文件。 (例如格式-1.0 0.0顶点A)我的任务是创建方法

public void read(InputStream is) throws IOException

这将保存顶点的X和Y值,然后将其标签为“顶点A”。 我不知道如何正确解析它:

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

我也需要创建方法

public void read(File file) throws IOException

这使得完全相同,但使用文件而不是流。 您能告诉我这两种方法之间的区别吗?

文件表示文件系统上的节点,流表示具有读取头的数据序列。 打开文件以读取会导致输入流。 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
}

我将执行以下操作并通过代码进行解释:)

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

您也可以通过使用StringTokenizer避免使用split,这是另一种方法:)。

如另一个答案所述,文件仅是文件系统中节点的表示,它仅指向文件系统中现有的元素,但此时内部文件中不包含任何数据或信息,我的意思是,仅作为文件提供信息(如果是文件,目录或类似文件)(如果不存在,则您将收到FileNotFoundException)。

InputStream是一个数据序列,此时,您应该在此处具有需要由BufferedReader,ObjectInputStream或其他组件处理或读取的信息,具体取决于您需要执行的操作。

有关更多信息,您还可以询问友好的API文档:

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

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

问候和...快乐编码:)

暂无
暂无

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

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