如何在java中将数组的字符串输出到文本文件

如何在java中将数组的字符串输出到文本文件,java,Java,我从一个文件中读取文本,然后使用并行字符串数组将其追加到另一个文件中,但我一直在了解错误市场。java:84:error:找不到合适的写入方法(string,string,string,string,string,string,string),我找不到修复它的方法。 我的节目: import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io

我从一个文件中读取文本,然后使用并行字符串数组将其追加到另一个文件中,但我一直在了解错误市场。java:84:error:找不到合适的写入方法(string,string,string,string,string,string,string),我找不到修复它的方法。 我的节目:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class Market {

    private FileWriter output;

    String[] market = new String[100];
    String[] name = new String[100];
    String[] street = new String[100];
    String[] city = new String[100];
    String[] state = new String[100];
    String[] country = new String[100];

    public void openFile() {
        try {
            output = new FileWriter("report.txt", true);
        } catch (SecurityException securityException) {
            System.err.println("You do not have write access to this file.");
            System.exit(1);
        } catch (FileNotFoundException fileNotFoundException) {
            System.err.println("Error opening or creating file.");
            System.exit(1);
        }
    }

    public void ReadMarket() {

        try {
            BufferedReader readbuffer = new BufferedReader(new FileReader("markets.txt"));
            String strRead;

            while ((strRead = readbuffer.readLine()) != null) {

                int i = 0;

                String splitarray[] = strRead.split("\t");
                String firstentry = splitarray[0];
                String secondentry = splitarray[1];
                String thirdentry = splitarray[2];
                String fourthentry = splitarray[3];
                String fithentry = splitarray[4];
                String sixthentry = splitarray[5];

                market[i] = firstentry;
                name[i] = secondentry;
                street[i] = thirdentry;
                city[i] = fourthentry;
                state[i] = fithentry;
                country[i] = sixthentry;

                Writer.write("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]);
                i++;
            }
        }

        catch (IOException e) {
            System.out.println("Not Working");
        }
    }

    public void closeFile() {
        output.close();
    }
}

我相信这是因为方法:

     Writer.write("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]); 

不存在,如果您知道存在这样的方法,请链接到javadoc

Writer.write
不接受您试图传递的参数量。其中一个
write
方法采用
字符串。因此,您可能希望将内容格式化为字符串,然后将其传递给
write
方法

试试这个:

Writer.write(String.format("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i])); 

这是因为
FileWriter
类中没有这样的方法。要以所需的格式写入数据,可以执行以下操作

String toWriteString = String.format("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]); // Use String format to get the String in the format you required and then write that to the file
output.write(toWriteString); // I believe output is the FileWriter object from what I see in the code

更改以下行:

Writer.write("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]);
致:

错误原因:

  • 类中的write()不是静态的,您试图将其用作静态的
  • 类中的write()没有接受可变数量参数的方法
  • 您已经创建了(输出)的实例,应该使用它来写入数据

  • 很好的解决方案,感谢您在解释错误的基础上提供它!
    output.write(String.format("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]));