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條經驗 獲得超4個贊
您可以按如下方式進行操作:
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。
添加回答
舉報