如何在Java中只读取两列文本文件并重新格式化?
我有一个如下所示的文本文件:如何在Java中只读取两列文本文件并重新格式化?,java,file,text,java.util.scanner,Java,File,Text,Java.util.scanner,我有一个如下所示的文本文件: House1 25456 22456 54564 54564 House2 54788 54756 House1 25456 House1 22456 House1 54564 House1 54564 House2 54788 House2 54756 House(again same principle) 房屋(最多可容纳1500栋房屋) 我希望它看起来像这样: House1 25456 22456 54564 54564 House2 54788
House1 25456 22456 54564 54564
House2 54788 54756
House1 25456
House1 22456
House1 54564
House1 54564
House2 54788
House2 54756
House(again same principle)
房屋(最多可容纳1500栋房屋)
我希望它看起来像这样:
House1 25456 22456 54564 54564
House2 54788 54756
House1 25456
House1 22456
House1 54564
House1 54564
House2 54788
House2 54756
House(again same principle)
代码
使用String.split()提取门牌号。然后循环遍历它们以打印它(或者在需要时将其收集到StringBuilder)。例如:
while(sc.hasNextLine()){
String[]tokens=sc.nextLine().split(\\s+);
字符串houseName=tokens[0];
for(int i=1;i
String[]parts=sc.nextLine().split();
对于(int i=1;i我试图使其尽可能简单,并且对原始代码的更改更少:
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class Houses {
public static void main(String[] args) {
UsingScanner();
}
public static void UsingScanner() {
try {
Scanner sc = new Scanner(new File("C:\\Test.txt"));
//fw is to write you desired result into the Output.txt file
FileWriter fw = new FileWriter(new File("C:\\Output.txt"));
while (sc.hasNextLine()) {
//split your line by spaces into an array of Strings
String[] splitted = sc.nextLine().split(" ");
//at index zero of the array there is always the house name
for (int k = 1; k < splitted.length; k++) { //so start the loop from index 1
//loop every numeric item of a line, write the house name and the numeric item
fw.write(splitted[0] + " " + splitted[k] + System.getProperty("line.separator"));
}
//step to the next house name (another line)
}
fw.close();
} catch (Exception e) {
System.out.println("Ups! you got a problem");
}
}
}
导入java.io.File;
导入java.io.FileWriter;
导入java.util.Scanner;
公屋{
公共静态void main(字符串[]args){
使用扫描仪();
}
使用scanner()的公共静态无效{
试一试{
Scanner sc=新的扫描仪(新文件(“C:\\Test.txt”);
//fw是将您想要的结果写入Output.txt文件
FileWriter fw=新的FileWriter(新文件(“C:\\Output.txt”);
while(sc.hasNextLine()){
//将行按空格拆分为字符串数组
String[]splitted=sc.nextLine().split(“”);
//在数组的索引0处,始终存在房屋名称
对于(intk=1;k
请使用camelCase作为方法名称。我使用了您的代码,如下所示,它工作得很好。但是我注意到,当House(Number)时,我们可能会有一个条件可能旁边没有任何代码。例如,house2没有任何代码:House1 25456 house2 House3 87888 88889 87724我想我必须在打印它们之前添加一个if条件。谢谢分享!最终目标是按照您的建议将其保存到一个新文档中。我已经为输出编写了代码。为简单起见,oonly put在system.out.println()中:我尝试了上面的文件编写器,它没有将数据保存在拆分的[0]空间中拆分[k]并返回新行。它正在做的是将所有内容打印在一行中。@joshua这可能是因为您使用的是Windows和字符“\n”(在以前的答案版本中,我在每个循环的末尾都添加了该选项)不足以实际呈现新行。现在我修改了答案,将“\n”替换为System.getProperty(“line.separator”)(它返回依赖于平台的行分隔符),应该可以工作。感谢您的评论!
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class Houses {
public static void main(String[] args) {
UsingScanner();
}
public static void UsingScanner() {
try {
Scanner sc = new Scanner(new File("C:\\Test.txt"));
//fw is to write you desired result into the Output.txt file
FileWriter fw = new FileWriter(new File("C:\\Output.txt"));
while (sc.hasNextLine()) {
//split your line by spaces into an array of Strings
String[] splitted = sc.nextLine().split(" ");
//at index zero of the array there is always the house name
for (int k = 1; k < splitted.length; k++) { //so start the loop from index 1
//loop every numeric item of a line, write the house name and the numeric item
fw.write(splitted[0] + " " + splitted[k] + System.getProperty("line.separator"));
}
//step to the next house name (another line)
}
fw.close();
} catch (Exception e) {
System.out.println("Ups! you got a problem");
}
}
}