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

解决方案 »

  1.   

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

  2.   

    以下是我程序代码,给你参考。  
    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();
              
      

  3.   

    两位大哥 小弟试过了   可是加上就出问题  运行不了  
    请个位帮我看看 我的程序是没做完  是这样的  在此前谢过了
    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){}
                }
            }
        }
    }