簡體   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