我正在尝试编写一个程序,该程序给出一个包含五个或更多人的姓名和地址的文件,为每个人创建一个不同的文件(字母)(新文件将被命名为将接收的人它)。 主文件的结构是这样的:
type1.0001 #n John Harrison #a Whatever Street, 490 - Liverpool
.... and so on
所以“type1”是这个人必须发送的字母类型,“#n”之后的字是名称,“#a”之后的字是地址。
我一直在尝试的是:
String datos = "main_file.txt";
String tipo1 = "type1.txt";
String tipo2 = "type2.txt";
String tipo3 = "type3.txt";
char[] type1 = {'t', 'i', 'p', 'o', '1'};
//all other types should be here
String line;
FileReader fr = new FileReader("mainfile.txt");
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
while ((line = br.readLine()) != ".") {
char[] lineArray = line.toCharArray();
if (lineArray == type1) {
//code that creates file type1
}
}
}
fr.close();
到目前为止,这只是决定发送哪个字母的代码,但它不起作用。 我认为这与“while”循环有关。
拜托,我1个月前开始使用Java,所以如果有人能帮助我,我会非常感激!
由于
现在,我有了这个:
FileReader fr = new FileReader("main_file.txt");
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
String nameMark = "#n";
String addressMark = "#a";
int nameStart = line.indexOf(nameMark) + nameMark.length();
int addressStart = line.indexOf(addressMark) + addressMark.length();
String name = line.substring(nameStart, addressStart - addressMark.length());
String address = line.substring(addressStart, line.length());
if (line.startsWith("tipo1.")) {
FileWriter fw = new FileWriter("file1.txt");
char[] vector = name.toCharArray();
int index = 0;
while (index < vector.length) {
fw.write(vector[index]);
index++;
}
fw.close();
} else if (line.startsWith("tipo2.")) {
FileWriter fw = new FileWriter("file1.txt");
char[] vector = name.toCharArray();
int index = 0;
while (index < vector.length) {
fw.write(vector[index]);
index++;
}
fw.close();
}
}
fr.close();
但它不起作用。
有人能帮助我吗?
答案 0 :(得分:0)
您不能将#!/usr/bin/env perl6
use v6;
my @a = 1 .. 4;
@a[5] = 6;
my @b;
for @a -> $i {
@b.push( ~$i );
}
say "=====\n" x 3;
与数组一起使用。 (嗯,你可以,但它不能达到你的预期。)也就是说,这一行是错误的:
==
请尝试 if (lineArray == type1)
:
Arrays.equals
答案 1 :(得分:0)
正如 Marc B 告诉你的那样,不要两次阅读这些行。
另外,只比较一下这行的开头,就会比你的char数组少得多。
要检索姓名和地址,您可以使用String的indexOf
和substring
方法。
以下是一个完整的例子:
while ((line = br.readLine()) != null) {
// get the name and the address of this line
String nameMark = "#n";
String addressMark = "#a";
int nameStart = line.indexOf(nameMark) + nameMark.length();
int addressStart = line.indexOf(addressMark) + addressMark.length();
String name = line.substring(nameStart, addressStart - addressMark.length());
String address = line.substring(addressStart, line.length());
// get the line type
if (line.startsWith("tipo1")) {
//code that creates file type1 with name and address
}
else if(line.startsWith("tipo2")) {
//code that creates file type2 with name and address
}
...
...
}
答案 2 :(得分:0)
首先尝试这个。
String line;
FileReader fr = new FileReader("mainfile.txt");
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) { // finish when line is null not "."
String [] parts = line.split("#");
String name, address;
if (parts.length > 2) {
name = parts[1].substring(2);
address = parts[2].substring(2);
}
if (line.startsWith("tipo1")) {
// save to tipo1 file
} else if (line.startsWith("tipo2")) {
// save to tipo2 file
} else if (line.startsWith("tipo2")) {
// save to tipo2 file
} else if (line.startsWith("tipo3")) {
// save to tipo2 file
} else if (line.startsWith("tipo4")) {
// save to tipo2 file
} else if (line.startsWith("tipo2")) {
// save to tipo2 file
}
}
fr.close();