3 回答
TA贡献1829条经验 获得超4个赞
您可以使用BufferedReader
.
public static void main(String[] args) throws IOException {
int size; // using it for first line
int rows; // using it for second line
int cols; // using it for third line
// pass the path to the file as a parameter
BufferedReader fr = new BufferedReader(
new FileReader("input1.txt")
);
// skipping 3 lines
fr.readLine();
fr.readLine();
fr.readLine();
String line = fr.readLine();
while (line != null) {
for (char c : line.toCharArray()) {
System.out.print(c + " ");
}
System.out.println();
line = fr.readLine();
}
}
TA贡献1872条经验 获得超3个赞
您可以按如下方式进行操作:
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
File file=new File("demo.txt");
Scanner sc = new Scanner(file,StandardCharsets.UTF_8.name());
//Ignore first three lines
int count=0;
while(count<3){
sc.nextLine();
count++;
}
//Add space after each character in the remaining lines
while(sc.hasNext()) {
String line=sc.nextLine();
char []chars=line.toCharArray();
for(char c:chars)
System.out.printf("%c ",c);
System.out.println();
}
}
}
输出:
T . T . . .
. . . . . .
. . . . T .
. . . . T .
T T T . . .
. . . . . .
TA贡献1784条经验 获得超8个赞
实际上,至少在 java 8 上,“System.out.print((char) i + " ");” 应该可以正常工作。我现在刚刚尝试过并且对我来说效果很好。你使用的是哪个java版本?否则你可以按照@second的建议尝试BufferedReader。
添加回答
举报