首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Java从文件中读取数据

使用Java从文件中读取数据
EN

Stack Overflow用户
提问于 2014-12-12 20:19:45
回答 2查看 80关注 0票数 1

我有一份文件,每一行都代表着顶点。(格式,例如-1.00.0 vertice )我的任务是创建方法

代码语言:javascript
运行
复制
public void read(InputStream is) throws IOException

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

代码语言:javascript
运行
复制
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();
          }
    }

此外,我还需要创建方法

代码语言:javascript
运行
复制
public void read(File file) throws IOException

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-12 20:59:53

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

代码语言:javascript
运行
复制
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避免使用拆分,这是另一种方法:)。

正如在另一个答案中提到的,文件只是文件系统中一个节点的表示,它只是指向文件系统中存在的一个元素,但它在您的内部文件的此时不保存任何数据或信息,我的意思是,只是一个文件的信息(如果是一个文件、目录或类似的东西)(如果它不存在,您将收到一个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

问候还有..。快乐编码:)

票数 0
EN

Stack Overflow用户

发布于 2014-12-12 20:24:56

文件代表文件系统上的一个节点,流是具有读取头的一系列数据的表示。打开用于读取的文件将导致输入流。System.In是一个没有提供文件的输入流的例子,它是stdin的流。

代码语言:javascript
运行
复制
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
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27451497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档