我正在从一个文件读取文本并使用并行字符串数组追加到另一个文件,但我不断收到错误Market.java:84:错误:找不到合适的写入方法(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();
}
}
答案 0 :(得分:1)
我相信那是因为方法:
Writer.write("%-30s%-20s%-30s%-20s%-30s%-20s\n", market[i], name[i], street[i], city[i], state[i], country[i]);
如果您知道存在这样的方法,请不要存在,请链接到javadoc!
答案 1 :(得分:1)
更改以下行:
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]));
错误原因:
答案 2 :(得分:0)
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]));
答案 3 :(得分:0)
那是因为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