java字节文件输入流读文件

时间:2023-02-24 20:50:49

在下面程序中,读取一个文本文件,并将其显示到对话框上。

在编写程序前必须知道两件事:

1、怎样把一个流与一个文件联系起来。

2、用什么方法把从文件中读取的数据显示到对话框中。

import java.io.*;
import javax.swing.JOptionPane;
class Example8_1{
public static void main(String args[]){
byte buffer[] = new byte[2056];
int array[]=new int[100];
int node_num,j=0,c=0;
int bytes;
String str;
String str1;
try{
File file = new File(args[0]);
FileInputStream fileInput = new FileInputStream(file);
bytes = fileInput.read(buffer);
str = new String(buffer,0,bytes);
}
catch(Exception e){
str = e.toString();
} JOptionPane.showMessageDialog(null,str);
System.exit(0);
}
}