Java 在文件中查找单词或字符
我试图编写一个程序,给定一个包含五个或更多人姓名和地址的文件,为每个人创建一个不同的文件(字母)(新文件将被命名为接收文件的人)。 主文件的结构如下所示:Java 在文件中查找单词或字符,java,file,loops,search,Java,File,Loops,Search,我试图编写一个程序,给定一个包含五个或更多人姓名和地址的文件,为每个人创建一个不同的文件(字母)(新文件将被命名为接收文件的人)。 主文件的结构如下所示: type1.0001 #n John Harrison #a Whatever Street, 490 - Liverpool .... and so on 因此,“type1”是这个人必须发送的信件类型,“#n”之后的单词是姓名,“#a”之后的单词是地址 我一直在尝试的是: String datos = "main_file.txt";
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”循环有关
拜托,我一个月前开始使用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();
FileReader fr=newfilereader(“main_file.txt”);
BufferedReader br=新的BufferedReader(fr);
而((line=br.readLine())!=null){
字符串nameMark=“#n”;
字符串addressMark=“#a”;
int nameStart=line.indexOf(nameMark)+nameMark.length();
int addressStart=line.indexOf(addressMark)+addressMark.length();
String name=line.substring(nameStart,addressStart-addressMark.length());
字符串地址=line.substring(addressStart,line.length());
if(行startsWith(“tipo1”)){
FileWriter fw=新的FileWriter(“file1.txt”);
char[]vector=name.toCharArray();
int指数=0;
while(索引<向量长度){
fw.write(向量[索引]);
索引++;
}
fw.close();
}else if(行startsWith(“tipo2”)){
FileWriter fw=新的FileWriter(“file1.txt”);
char[]vector=name.toCharArray();
int指数=0;
while(索引<向量长度){
fw.write(向量[索引]);
索引++;
}
fw.close();
}
}
fr.close();
但它不起作用
有人能帮我吗?你不能对数组使用
=
。(好吧,你可以,但它没有达到你的预期。)也就是说,这句话是错误的:
if (lineArray == type1)
尝试数组。改为等于:
if (Arrays.equals(lineArray, type1))
正如Marc B告诉你的,不要把台词读两遍
另外,仅仅比较行的开头,就不会比使用char数组的东西有过多的杀伤力
要检索姓名和地址,可以使用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
}
...
...
}
先试试这个
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();
您有两个
readLine()
调用,因此在外部循环中获取一行,然后获取另一行,丢弃第一行和第二行循环。因此,您的外循环将只获取一行,这将被丢弃,然后内循环将处理文件的其余部分,不会为外循环留下任何内容。谢谢您的帮助!!现在我怎么读每个人的名字和地址?。我需要在我编辑的每个新文件中都包含这两个问题,有人能检查一下吗?你真的是这本书的作者吗?谢谢你的帮助!!现在我怎么读每个人的名字和地址?。我需要在每个新文件中都包含它们谢谢你的帮助!!现在我怎么读每个人的名字和地址?。我需要在每个新文件中都包含它们。我刚刚添加了一些示例来检索您的字段。我必须为每个人编写这个,还是只编写一次?如果在循环中只编写一次,它将应用于每个读取行(请参见编辑的代码)。我正在尝试运行它,但它会给我以下信息:线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-15