怎样用Java读取txt文件中每行的第9个字符?请给出代码,谢谢~~
2个回答
2017-03-03
展开全部
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();
}
}
我累累的码完,发现已经有人写好了,看起来很专业。我是这两天刚学到流,顺便练习下,我也来学习下高人的代码!!!
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();
}
}
我累累的码完,发现已经有人写好了,看起来很专业。我是这两天刚学到流,顺便练习下,我也来学习下高人的代码!!!
展开全部
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;
}
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;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询