Java 如何将从文件读取的字符串添加到ArrayList?

Java 如何将从文件读取的字符串添加到ArrayList?,java,Java,我有一个超级初学者的问题。我今天有一个计算机科学考试,其中一个实践问题是: 编写一个执行以下任务的程序: 打开名为hello.txt的文件 将消息“Hello,World!”存储在文件中 关闭文件 再次打开同一个文件 将消息读入字符串变量并打印 这是我目前为止的代码: import java.util.*; import java.io.*; public class ReadFile { public static void main(String[] args) throws

我有一个超级初学者的问题。我今天有一个计算机科学考试,其中一个实践问题是:

  • 编写一个执行以下任务的程序:
  • 打开名为hello.txt的文件
  • 将消息“Hello,World!”存储在文件中
  • 关闭文件
  • 再次打开同一个文件
  • 将消息读入字符串变量并打印
这是我目前为止的代码:

import java.util.*;
import java.io.*;

public class ReadFile
{
    public static void main(String[] args) throws FileNotFoundException
    {
        PrintWriter out = new PrintWriter("hello.txt");
        out.println("Hello, World");
        File readFile = new File("hello.txt");
        Scanner in = new Scanner(readFile);
        ArrayList<String> x = new ArrayList<String>();
        int y = 0;

        while (in.hasNext())
        {
            x.add(in.next());
            y++;
        }

        if (x.size() == 0)
        {
            System.out.println("Empty.");
        }
        else
        {
            System.out.println(x.get(y));
        }

        in.close();
        out.close();     
    }
}
import java.util.*;
导入java.io.*;
公共类读取文件
{
公共静态void main(字符串[]args)引发FileNotFoundException
{
PrintWriter out=新的PrintWriter(“hello.txt”);
out.println(“你好,世界”);
File readFile=新文件(“hello.txt”);
扫描仪输入=新扫描仪(读取文件);
ArrayList x=新的ArrayList();
int y=0;
while(在.hasNext()中)
{
x、 添加(in.next());
y++;
}
如果(x.size()==0)
{
System.out.println(“空”);
}
其他的
{
系统输出println(x.get(y));
}
in.close();
out.close();
}
}

这段代码有什么问题?

我想这段代码的问题在于你无法从文件中读取任何内容

这是因为它是缓冲的

fileName—用作此编写器目标的文件名。如果文件存在,那么它将被截断为零大小;否则,将创建一个新文件。输出将写入文件,并被缓冲

您需要先关闭刚写入的文件,然后再打开该文件进行读取,以便将更改转移到物理存储中。因此将
移出.close()
就在out.println(“你好,世界”)

1)您需要关闭流

2) 您需要使用(y-1)引用x Arraylist,否则您将得到 a
java.lang.IndexOutOfBoundsException
。索引从0开始,而不是从1开始

publicstaticvoidmain(字符串[]args)抛出FileNotFoundException
{
PrintWriter out=新的PrintWriter(“hello.txt”);
out.println(“你好,世界”);
out.close();
File readFile=新文件(“hello.txt”);
扫描仪输入=新扫描仪(读取文件);
ArrayList x=新的ArrayList();
int y=0;
while(在.hasNext()中)
{
x、 添加(in.next());
y++;
}
in.close();
如果(x.size()==0)
{
System.out.println(“空”);
}
其他的
{
系统输出println(x.get(y-1));
}
}
}

为什么您认为有什么问题?您是否得到错误或错误的输出?y将保留添加元素的数量。要访问最后一个流,您必须在get调用中使用y-1。我得到的输出是“Empty”,而不是“Hello,World”。您可能必须在读取该文件之前关闭输出流。您需要先
out.close()
将更改提交到该文件
   public static void main(String[] args) throws FileNotFoundException
        {
            PrintWriter out = new PrintWriter("hello.txt");
            out.println("Hello, World");
            out.close();
            File readFile = new File("hello.txt");
            Scanner in = new Scanner(readFile);
            ArrayList<String> x = new ArrayList<String>();
            int y = 0;

            while (in.hasNext())
            {
                x.add(in.next());
                y++;
            }

            in.close();  

            if (x.size() == 0)
            {
                System.out.println("Empty.");
            }
            else
            {
                System.out.println(x.get(y-1));
            }

        }
    }
class FileWritingDemo {
public static void main(String [] args) {
char[] in = new char[13]; // to store input
int size = 0;
try {
File file = new File("MyFile.txt"); // just an object

FileWriter fw = new FileWriter(file); // create an actual file & a FileWriter obj
fw.write("Hello, World!"); // write characters to the file
fw.flush(); // flush before closing
fw.close(); // close file when done

FileReader fr = new FileReader(file); // create a FileReader object
size = fr.read(in); // read the whole file!
for(char c : in) // print the array
System.out.print(c);
fr.close(); // again, always close
} catch(IOException e) { }
}
}