怎么样利用InputStream 一行一行的读取文件 每行只读取前几个字节啊!!!! 怎么样利用InputStream 一行一行的读取文件 每行只读取前几个字节啊!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 为什么用inputstream 你要再套一层bufferedreaderbufferdereader br=new BufferdeReader(new InputStream("文件名"));if(String s=br.readline()!=null );读一行,如果不到文件末尾string temp=s.substring(0,3);//截取前4个 以下是我程序代码,给你参考。 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(); 两位大哥 小弟试过了 可是加上就出问题 运行不了 请个位帮我看看 我的程序是没做完 是这样的 在此前谢过了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){} } } }} 求助啊,十万火急的面试题,关于FOR循环,高手帮帮忙,谢谢 有没有做snmp网管软件的?探讨下这个职业的前途问题 求jsf做的程序做学习参考! 我的网站建成了大家看看怎么样..给个评价 我的简单的程序为什么不能显示 如何生成一个带圆角的button 请问一下为什么不显示 我用java调用*.dll的时候为什么结果没出来呢,高手你们看看啊,急急急急!!!!!!!! WebLogic、Corba、M3IdlToJava,有知道的请赐教! OCX控件和网页的交互 早上好啊!请教各位牛哥一个匿名类的语法问题?谢谢!!! 谁知道Java怎么开n次方根???急
bufferdereader br=new BufferdeReader(new InputStream("文件名"));
if(String s=br.readline()!=null );读一行,如果不到文件末尾
string temp=s.substring(0,3);//截取前4个
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();
请个位帮我看看 我的程序是没做完 是这样的 在此前谢过了
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){}
}
}
}
}