简体   繁体   English

在java中读取文件的问题

[英]Problems with reading a file in java

I'm doing a project for a class, but for the life of me I'm having the hardest time figuring out how to read text from a file. 我正在为一个班级做一个项目,但对于我的生活,我最难找到如何从文件中读取文本。 We have to create a traffic light that queues trucks and cars coming from North, South, East, and West. 我们必须创建一个交通灯,对来自北,南,东和西的卡车和汽车进行排队。 It's been a long time since I've done any coding, so I'm struggling immensely. 自从我完成任何编码以来已经有很长一段时间了,所以我在努力奋斗。 I think it just reads the memory location. 我认为它只是读取内存位置。 Here's my code for reading in a file. 这是我在文件中读取的代码。

package Project1;
import java.io.*;
import java.util.*;


public class TrafficSim {

public String input;

public TrafficSim(String input)
{
    this.input = input;
    readFromFile();
}



private boolean readFromFile()
{
    File inputText = new File("input1.txt");

    try
    {
        Scanner scan = new Scanner(inputText);

        while(scan.hasNextLine())
        {
            String direction = scan.nextLine();
            int num = scan.nextInt();
        }
    }
    catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }
    return false;
}



public static void main(String[] args) {
    // TODO Auto-generated method stub      
    TrafficSim sim = new TrafficSim("input1.txt");
        System.out.println(sim);    
}

}

Your method readFromFile sure enough reads from a file, but then it doesn't do anything. 你的方法readFromFile确实从文件读取,但它没有做任何事情。 All you do is read line by line, storing a line of text and an int in variables which are forgotten after each iteration of your while loop. 你所做的就是逐行读取,在你的while循环的每次迭代之后,在变量中存储一行文本和一个int。

Your code System.out.println(sim) prints out whatever the toString method of your class returns, and since you didn't override that method it will print out the result of Object.toString , which is not what you want. 您的代码System.out.println(sim)打印出您的类返回的任何toString方法,并且由于您没有覆盖该方法,它将打印出Object.toString的结果,这不是您想要的。

To put it simply, you're reading from a file but you're not doing anything with the contents that you read. 简单来说,你是从一个文件中读取,但是你没有对你读过的内容做任何事情。

Here is what I would do.... 这就是我要做的......

public class TrafficSim {

    private String input;
    private String content;

    public TrafficSim(String input) {
        this.setInput(input);
        this.setContent(readFromFile());
    }

    private String readFromFile() {
        File inputText = new File(input);
        StringBuilder sb = new StringBuilder();

        try {
            Scanner scan = new Scanner(inputText);
            while (scan.hasNextLine()) {
                sb.append(scan.nextLine());
            }
            scan.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }

    public static void main(String[] args) {
        TrafficSim sim = new TrafficSim("input1.txt");
        System.out.println(sim.getContent());
    }
}

The issue I see though is that you're not following the comments and suggestions already made. 我看到的问题是你没有遵循已经提出的意见和建议。 ktm5124 was pretty clear on what the problem is. ktm5124很清楚问题是什么。 At some point you're going to have to understand what is going on here and how to fix it. 在某些时候,你将不得不了解这里发生了什么,以及如何解决它。

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

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