怎样用Java读取txt文件中每行的第9个字符?请给出代码,谢谢~~

如题所述

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test1 {
public static void main(String[] args) throws IOException{
File file = new File("h:/test.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String str;
while((str = br.readLine()) != null){
char ch = str.charAt(8);
System.out.println(ch);
}
br.close();
}
}
我累累的码完,发现已经有人写好了,看起来很专业。我是这两天刚学到流,顺便练习下,我也来学习下高人的代码!!!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-03-03
public static HashMap<String,String> read(String path){
HashMap<String,String> res = new HashMap<String,String>();
try {
BufferedReader read = new BufferedReader(new FileReader(new File(path)));
String buff = null;
int i=1;
while(null!=(buff=read.readLine())){
String v = buff.length()>8?(buff.charAt(8)+""):null;
res.put(i+"", v);
i++;
}
read.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}本回答被网友采纳
相似回答