zoukankan      html  css  js  c++  java
  • java文件读取

      1 package textopen;
      2 
      3 import java.awt.BorderLayout;
      4 import java.awt.EventQueue;
      5 import java.awt.event.ActionEvent;
      6 import java.awt.event.ActionListener;
      7 import java.io.File;
      8 import java.io.FileInputStream;
      9 import java.io.FileNotFoundException;
     10 import java.io.IOException;
     11 
     12 import javax.swing.JFrame;
     13 import javax.swing.JPanel;
     14 import javax.swing.border.EmptyBorder;
     15 import javax.swing.JButton;
     16 import java.awt.CardLayout;
     17 import javax.swing.JTextField;
     18 import javax.swing.JScrollPane;
     19 import javax.swing.JTextArea;
     20 import java.awt.GridLayout;
     21 
     22 public class textopen1 extends JFrame implements ActionListener{
     23 
     24     private JPanel contentPane;
     25     private JButton openButton;
     26     private JPanel panel;
     27     private Object i;
     28     private JScrollPane scrollPane;
     29     private JPanel panel_1;
     30     private JTextArea text;
     31 
     32     /**
     33      * Launch the application.
     34      */
     35     public static void main(String[] args) {
     36         EventQueue.invokeLater(new Runnable() {
     37             public void run() {
     38                 try {
     39                     textopen1 frame = new textopen1();
     40                     frame.setVisible(true);
     41                 } catch (Exception e) {
     42                     e.printStackTrace();
     43                 }
     44             }
     45         });
     46     }
     47 
     48     /**
     49      * Create the frame.
     50      */
     51     public textopen1() {
     52         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     53         setBounds(100, 100, 595, 523);
     54         contentPane = new JPanel();
     55         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
     56         contentPane.setLayout(new BorderLayout(0, 0));
     57         setContentPane(contentPane);
     58         
     59         panel = new JPanel();
     60         contentPane.add(panel, BorderLayout.SOUTH);
     61         
     62         openButton = new JButton("u786Eu5B9A");
     63         openButton.addActionListener(this);
     64         panel.add(openButton);
     65         
     66         scrollPane = new JScrollPane();
     67         contentPane.add(scrollPane, BorderLayout.CENTER);
     68         
     69         panel_1 = new JPanel();
     70         scrollPane.setViewportView(panel_1);
     71         panel_1.setLayout(new CardLayout(0, 0));
     72         
     73         text = new JTextArea();
     74         panel_1.add(text, "name_30136914873902");
     75         
     76         
     77     }
     78     public static void open(){
     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() throws Exception
    108     {
    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(new String(b));
    119         //关闭流
    120        fi.close();
    121        }
    122     @Override
    123     public void actionPerformed(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 }

     单击读取文件按钮的运行的结果

  • 相关阅读:
    根据条件把一个字符串拆分成几个字符串
    【SQLServer】SQL Server 2000的分页方法(SQL篇)
    【C#】NHibernate下实现SQL2000分页(SQL篇)
    【Oracle】仿Oracle Sequence的自定义年份Sequence(适合任何数据库)
    【Oracle】仿Oracle Sequence的自定义年份Sequence(适合任何数据库)(续)
    【C#】NHibernate下为SQL Server添加随机排序
    【LoadRunner】LoadRunner 9.5回放Windows Socket时CPU 100%
    【CSS】CSS Cookbook例子:创建水平导航菜单
    【CSS】CSS Cookbook例子:创建文字导航菜单和翻转特效
    【Flex】实现模块顺序执行
  • 原文地址:https://www.cnblogs.com/jiangxifanzhouyudu/p/6049147.html
Copyright © 2011-2022 走看看