gpt4 book ai didi

java - 逐字符读取文本文件

转载 作者:行者123 更新时间:2023-12-01 09:17:03 25 4
gpt4 key购买 nike

我需要帮助从文本文件中读取字符。我想逐个字符读取并获取ecpace的id

这是我的代码:

public class GenieL {
String Nom_of_TACHE;
int ID ;
int time;
Array table[];
public static void main(String[] args) throws IOException {
BufferedReader br=null;
String strLine="";

try {
br=new BufferedReader (new FileReader("my.txt"));

while((strLine=br.readLine())!=null){
System.out.println(strLine);
Nom_of_TACHE=parseint(strLine);//
}
} catch (FileNotFoundException ex) {
System.err.println("Unable to find the file name ");
}
catch (IOException e) {
System.err.println("Unable to read find the file name ");
}
}}

最佳答案

逐行读取文件,由于每一行都是一个字符串,因此对其上的字符执行 for 循环...

public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("./Details.txt"), "UTF8"));

String line;
while ((line = br.readLine()) != null) {
for (int i = 0; i < line.length(); i++) {
System.out.println("Char at: " + line.charAt(i));
}
System.out.println("Another line");
}
br.close();

} catch (IOException e) {
e.printStackTrace();
}
}

关于java - 逐字符读取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40471698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com