怎么样利用InputStream 一行一行的读取文件 每行只读取前几个字节啊!!!!

shangheyu 2005-12-27 04:46:38
怎么样利用InputStream 一行一行的读取文件 每行只读取前几个字节啊!!!!
...全文
3957 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
shangheyu 2005-12-29
  • 打赏
  • 举报
回复 1
自己顶
linda_gj 2005-12-28
  • 打赏
  • 举报
回复
以下是我程序代码,给你参考。
FileReader fr = new FileReader(FileAddress);
BufferedReader buff = new BufferedReader(fr);

while (buff.ready()) {
line = buff.readLine();
......
string tmp = line.substring(0,7) //根据需要截取(8位)
}
fr.close(); //别忘了关闭流,不然会对读取造成影响
buff.close();




shangheyu 2005-12-28
  • 打赏
  • 举报
回复
各位大下 快帮帮 小弟吧

shangheyu 2005-12-28
  • 打赏
  • 举报
回复
两位大哥 小弟试过了   可是加上就出问题  运行不了  
请个位帮我看看 我的程序是没做完  是这样的  在此前谢过了
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

class PMInputStreamFixed implements ActionListener,Runnable
{
JFrame f = null;
JLabel label = null;
JTextArea textarea = null;
JFileChooser fileChooser = null;

public PMInputStreamFixed()
{
f = new JFrame("ProgressMonitorInputStream Example");
Container contentPane = f.getContentPane();
textarea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textarea);
scrollPane.setPreferredSize(new Dimension(350,350));
JButton b = new JButton("读取文件");
b.addActionListener(this);
label = new JLabel(" ",JLabel.CENTER);

fileChooser = new JFileChooser();

contentPane.add(label,BorderLayout.NORTH);
contentPane.add(scrollPane,BorderLayout.CENTER);
contentPane.add(b,BorderLayout.SOUTH);

f.pack();
f.setVisible(true);

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public static void main(String[] args) {
new PMInputStreamFixed();
}

Thread athread;

public void actionPerformed(ActionEvent e)
{
athread = new Thread(this);
athread.start();
}

public void run()
{
File file = null;
int result = fileChooser.showOpenDialog(f);

textarea.setText("");

if (result == JFileChooser.APPROVE_OPTION)
{
file = fileChooser.getSelectedFile();
label.setText("您选择的文件名称为:"+file.getName());
}
else if(result == JFileChooser.CANCEL_OPTION)
{
label.setText("您没有选择任何文件");
}

FileInputStream inputStream;

if(file != null)
{
try{
inputStream = new FileInputStream(file);
}catch(FileNotFoundException fe){
label.setText("File Not Found");
return;
}

ProgressMonitorInputStream pmInputStream = new
ProgressMonitorInputStream(f, //parant component
"Get File Content.....", //message
inputStream); //input stream

ProgressMonitor pMonitor =
pmInputStream.getProgressMonitor();
pMonitor.setMillisToDecideToPopup(10);
pMonitor.setMillisToPopup(0);
int readbyte;

try{
while( (readbyte = pmInputStream.read()) != -1)
{
textarea.append(String.valueOf((char)readbyte));

try{
Thread.sleep(10);
}catch(InterruptedException ie){}

if(pMonitor.isCanceled())
{
textarea.append("\n\n读取文件中断!!");
}
}
}catch(IOException ioe){
label.setText("读取文件错误");
}
finally{
try{
if(pmInputStream != null)
pmInputStream.close();
}catch(IOException ioe2){}
}
}
}
}


undeadman 2005-12-27
  • 打赏
  • 举报
回复
为什么用inputstream 你要再套一层bufferedreader
bufferdereader br=new BufferdeReader(new InputStream("文件名"));
if(String s=br.readline()!=null );读一行,如果不到文件末尾
string temp=s.substring(0,3);//截取前4个

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧