读取.java的文件_java文件读取

1 packagetextopen;2

3 importjava.awt.BorderLayout;4 importjava.awt.EventQueue;5 importjava.awt.event.ActionEvent;6 importjava.awt.event.ActionListener;7 importjava.io.File;8 importjava.io.FileInputStream;9 importjava.io.FileNotFoundException;10 importjava.io.IOException;11

12 importjavax.swing.JFrame;13 importjavax.swing.JPanel;14 importjavax.swing.border.EmptyBorder;15 importjavax.swing.JButton;16 importjava.awt.CardLayout;17 importjavax.swing.JTextField;18 importjavax.swing.JScrollPane;19 importjavax.swing.JTextArea;20 importjava.awt.GridLayout;21

22 public class textopen1 extends JFrame implementsActionListener{23

24 privateJPanel contentPane;25 privateJButton openButton;26 privateJPanel panel;27 privateObject i;28 privateJScrollPane scrollPane;29 privateJPanel panel_1;30 privateJTextArea text;31

32 /**

33 * Launch the application.34 */

35 public static voidmain(String[] args) {36 EventQueue.invokeLater(newRunnable() {37 public voidrun() {38 try{39 textopen1 frame = newtextopen1();40 frame.setVisible(true);41 } catch(Exception e) {42 e.printStackTrace();43 }44 }45 });46 }47

48 /**

49 * Create the frame.50 */

51 publictextopen1() {52 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);53 setBounds(100, 100, 595, 523);54 contentPane = newJPanel();55 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));56 contentPane.setLayout(new BorderLayout(0, 0));57 setContentPane(contentPane);58

59 panel = newJPanel();60 contentPane.add(panel, BorderLayout.SOUTH);61

62 openButton = new JButton("\u786E\u5B9A");63 openButton.addActionListener(this);64 panel.add(openButton);65

66 scrollPane = newJScrollPane();67 contentPane.add(scrollPane, BorderLayout.CENTER);68

69 panel_1 = newJPanel();70 scrollPane.setViewportView(panel_1);71 panel_1.setLayout(new CardLayout(0, 0));72

73 text = newJTextArea();74 panel_1.add(text, "name_30136914873902");75

76

77 }78 public static voidopen(){79 FileInputStream fis=null;80 int i=0;81 try{82 fis=new FileInputStream("F:/java资源/java中线程.txt");83 } catch(FileNotFoundException e1) {84 //TODO 自动生成的 catch 块

85 e1.printStackTrace();86 }87 //System.out.println(fis);

88 try{89

90 fis.read();91 } catch(IOException e1) {92 //TODO 自动生成的 catch 块

93 e1.printStackTrace();94 }95

96 //当i=-1时,文件到达文件尾部

97 while(i !=-1){98 try{99 i=fis.read();100 } catch(IOException e1) {101 //TODO 自动生成的 catch 块

102 e1.printStackTrace();103 }104 }105 }106

107 public void input() throwsException108 {109 File file = new File("F:/java资源/java中线程.txt");//读取文件

110 FileInputStream fi = new FileInputStream(file);//创建字节流,打开该 文件

111 byte[] b = new byte[fi.available()];//fi.available 可以获取文件占多少字节

112 int a = -1;113 while((a= fi.read(b))!= -1)//判断文件是否到达文件末尾

114 {115 //System.out.println(new String(b));

116 }117 //System.out.println(new String(b));

118 text.setText(newString(b));119 //关闭流

120 fi.close();121 }122 @Override123 public voidactionPerformed(ActionEvent e) {124 //TODO 自动生成的方法存根

125 if(e.getSource()==openButton){126 try{127 input();128 } catch(Exception e1) {129 //TODO 自动生成的 catch 块

130 e1.printStackTrace();131 }132 }133 }134 }

8008e90538326258eb1612848a3913ff.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值