简体   繁体   English

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

[英]how to output strings of an array to a text file in java

I am reading text from one file and appending to another file using parallel string arrays, but I keep getting the error Market.java:84: error: no suitable method found for write(String,String,String,String,String,String,String) and I can't find a way to fix it. 我正在从一个文件读取文本,并使用并行字符串数组将其追加到另一个文件,但是我不断收到错误Market.java:84:错误:找不到适合write(String,String,String,String,String,String,字符串),我找不到解决它的方法。 My Program: 我的程序:

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();
    }
}

I believe that's because the method: 我相信是因为该方法:

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

Doesn't exist, please link to a javadoc if you know of such a method existing! 不存在,如果知道存在这样的方法,请链接到javadoc!

Change the following line: 更改以下行:

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

to: 至:

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

Reason for the error: 错误原因:

  1. write() in Writer class is not static and you are trying to use it as static. Writer类中的write()不是静态的,您正尝试将其用作静态。
  2. write() in Writer class does not have a method to take variable number of arguments. Writer类中的write()没有采用可变数量的参数的方法。
  3. You have created instance of FileWriter (output) and you should use it to write the data. 您已经创建FileWriter (输出)的实例,应该使用它来写入数据。

Writer.write does not take the amount of parameters you are trying to pass. Writer.write不会占用您尝试传递的参数数量。 One of the write method takes a String . write方法之一采用String So you may want to format the contents in a string and then pass on to write method. 因此,您可能希望将内容格式化为字符串,然后传递给write方法。

Try this: 尝试这个:

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

That's because there is no such method in the FileWriter class. 那是因为FileWriter类中没有这样的方法。 To write the data in the format you require, you can do something like this 要以所需的格式写入数据,可以执行以下操作

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

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

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