繁体   English   中英

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

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

我正在从一个文件读取文本,并使用并行字符串数组将其追加到另一个文件,但是我不断收到错误Market.java:84:错误:找不到适合write(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("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]);

至:

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

错误原因:

  1. Writer类中的write()不是静态的,您正尝试将其用作静态。
  2. Writer类中的write()没有采用可变数量的参数的方法。
  3. 您已经创建FileWriter (输出)的实例,应该使用它来写入数据。

Writer.write不会占用您尝试传递的参数数量。 write方法之一采用String 因此,您可能希望将内容格式化为字符串,然后传递给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

暂无
暂无

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

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