写一个java小程序使其能够从txt文件中一次读取一个字符的操作,怎么写?最好详细点
3个回答
展开全部
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(filename));
int chr = -1;
// isr.read()方法:返回读取的字符,如果已到达流的末尾,则返回 -1
while ((chr = isr.read()) != -1) {
// ...
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStreamReader isr = new InputStreamReader(new FileInputStream(filename));
int chr = -1;
// isr.read()方法:返回读取的字符,如果已到达流的末尾,则返回 -1
while ((chr = isr.read()) != -1) {
// ...
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2013-02-26
展开全部
FileInputStream
每次读多少,视乎逻辑需求的,read()就是读一个
每次读多少,视乎逻辑需求的,read()就是读一个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
@Test
public void read() throws IOException{
String path = "your_file_path";
File file = new File(path);
if(!file.exists()){
throw new IllegalArgumentException("File path error!");
}
int i;
InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
while((i=reader.read())!=-1){
System.out.println((char)i);
}
}
public void read() throws IOException{
String path = "your_file_path";
File file = new File(path);
if(!file.exists()){
throw new IllegalArgumentException("File path error!");
}
int i;
InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
while((i=reader.read())!=-1){
System.out.println((char)i);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询