我必须从文本文件中读取并格式化输入。我刚开始从java读取文件,我不知道如何处理我在这里读到的一些部分:http://pastebin.com/D0paWtAd
我必须在另一个文件中写入以下输出: Average,Joe,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();
}发布于 2015-04-27 15:36:45
如果您的目标是在另一个文件中执行指定的输出(您不需要在处理之前先在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,显然有一个异常执行我的示例,而它对我很好。这只能意味着在数据级别有一个错误。下面是我使用的数据示例(我相信我准确地复制了您共享的数据)

其结果是:

发布于 2015-04-27 15:35:19
很高兴知道您正在使用"StringBuilder“组件,而不是连接您的字符串值,方法是:)。
除了了解如何处理文件外,您还需要一些逻辑来获得预期的结果。这里我带了一种方法,可以帮助你,不是完美的,但可以告诉你如何面对这个问题。
//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();
}希望这能帮到你。编码愉快!
https://stackoverflow.com/questions/29899326
复制相似问题