Java:无法将ArrayList <long>写入文本文件</long>

时间:2014-12-17 07:35:38

标签: java arraylist io

我将size_list定义为Long的ArrayList。我已经import java.io.*;了。不知何故,以下仍然给了我一个错误。我做的和我遇到的其他帖子一样。有人出了什么问题吗?

    ArrayList<Long> size_list = some_method();
    try{
        FileWriter fileWriter = new FileWriter("results.txt");
        BufferedWriter out = new BufferedWriter(fileWriter);
        for(int i = 0; i < size_list.size(); i++ ){
            out.write(size_list.get(i));
            out.newline();
        }
        out.close();
    } catch(IOException ex){
        System.out.println("Error writing to file " );
    }

我收到的错误消息:

 no suitable method found for write(Long)
                out.write(size_list.get(i));
                   ^
    method Writer.write(int) is not applicable
      (argument mismatch; Long cannot be converted to int)
    method Writer.write(char[]) is not applicable
      (argument mismatch; Long cannot be converted to char[])
    method Writer.write(String) is not applicable
      (argument mismatch; Long cannot be converted to String)
    method BufferedWriter.write(int) is not applicable
      (argument mismatch; Long cannot be converted to int)
SCCsAlgo.java:287: error: cannot find symbol
                out.newline();
                   ^
  symbol:   method newline()
  location: variable out of type BufferedWriter

2 个答案:

答案 0 :(得分:1)

你没有像

这样的方法
 BufferedWriter.write(Long);

而不是这样你可以把它转换成String并写成:

out.write(size_list.get(i).toString() + "\n");

你可以摆脱换行符(你在那里输入你应该使用newLine note L in caps)方法。

答案 1 :(得分:1)

out.write(size_list.get(i).toString());
out.newLine();

而不是

out.write(size_list.get(i));
out.newline();

Java是Case sensitive。所以没有更多的方法可以在BufferedWriter中编写Long,也没有像newline();

这样的方法