我必须从文本文件中读取并格式化输入。我是从文件中读取Java的新手,我不知道如何处理我读过的部分内容 这是初始文件:http://pastebin.com/D0paWtAd
我必须在另一个文件中写下以下输出: 平均,乔,44,31,18,12,9,10
我设法从文件中取出所有内容并将其打印到输出。我需要帮助才能获得我需要的输出并将其打印到屏幕上。任何帮助表示赞赏。
这就是我现在写的:
public class FileParsing {
public static String
read(String filename) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Bogdi\\Desktop\\example.txt"));
String s;
StringBuilder sb = new StringBuilder();
while((s = in.readLine())!= null) sb.append(s + "\n");
in.close();
return sb.toString();
}
答案 0 :(得分:0)
很高兴知道你正在使用“StringBuilder”组件而不是连接你的String值,方法去:)。
除了了解Java.IO API以处理文件之外,您还需要一些逻辑来获得您期望的结果。在这里,我提出了一种方法,可以帮助你,而不是完美,但可以指出你如何面对这个问题。
//Reference to your file
String myFilePath = "c:/dev/myFile.txt";
File myFile = new File(myFilePath);
//Create a buffered reader, which is a good start
BufferedReader breader = new BufferedReader(new FileReader(myFile));
//Define this variable called line that will evaluate each line of our file
String line = null;
//I will use a StringBuilder to append the information I need
StringBuilder appender = new StringBuilder();
while ((line = breader.readLine()) != null) {
//First, I will obtain the characters after "equals" sign
String afterEquals = line.substring(line.indexOf("=") + 1, line.length());
//Then, if it contains digits...
if (afterEquals.matches(".*\\d+.*")) {
//I will just get the digits from the line
afterEquals = afterEquals.replaceAll("\\D+","");
}
//Finally, append the contents
appender.append(afterEquals);
appender.append(",");//This is the comma you want to include
}
//I will delete the last comma
appender.deleteCharAt(appender.length() - 1);
//Close the reader...
breader.close();
//Then create a process to write the content
BufferedWriter myWriter = new BufferedWriter(new FileWriter(new File("myResultFile.txt")));
//Write the full contents I get from my appender :)
myWriter.write(appender.toString());
//Close the writer
myWriter.close();
}
希望这可以帮到你。快乐的编码!
答案 1 :(得分:0)
如果您的目标是在另一个文件中执行指定的输出,则在处理之前不需要首先在StringBuilder中获取文件的内容,您可以将处理后的数据直接附加到StringBuilder中,然后就可以了将结果写入文件。这是一个适用于给定文件的示例,但如果密钥将来发生变化,您可能需要修改它:
以下方法将正确处理文件中的数据
public static String read(String filename) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(filename));
String s;
StringBuilder sb = new StringBuilder();
while((s = in.readLine())!= null) {
String[] split1 = s.split("=");
if (split1[0].equals("name")) {
StringTokenizer tokenizer = new StringTokenizer(split1[1]);
sb.append(tokenizer.nextToken());
sb.append(",");
sb.append(tokenizer.nextToken());
sb.append(",");
} else if (split1[0].equals("index")) {
sb.append(split1[1] + ",");
} else if (split1[0].equals("FBid")) {
sb.append(split1[1]);
} else {
StringTokenizer tokenizer = new StringTokenizer(split1[1]);
String wasted = tokenizer.nextToken();
sb.append(tokenizer.nextToken() + ",");
}
}
in.close();
return sb.toString();
}
下一个方法会将任何字符串读取到文件
public static void writeStringToFile(String string, String filePath) throws IOException {
BufferedWriter writer = new BufferedWriter(
new FileWriter(
new File(filePath)
)
);
writer.write(string);
writer.newLine();
writer.flush();
writer.close();
}
这是一个简单的测试(File1.txt包含您在粘贴bin上共享的文件中的数据,我将它们写在另一个文件中)
public static void main(String[] args) throws Exception {
String datas = read("C:\\Tests\\File1.txt");
System.out.println(datas);
writeStringToFile(datas, "C:\\Tests\\FileOuput.txt" );
}
它将产生您期望的确切输出
[编辑] @idk,显然你执行我的例子有异常,而它对我来说很好。这可能只意味着数据级别存在错误。这是我使用的数据样本(我相信我完全复制了您共享的数据)
结果如下: