Java GUI编程 输入输出流的一些注意点

关于各容器的默认布局:JPanel:FlowLayout JFrame:BorderLayout JDialog:BorderLayout

关于处理事件所在的包:除了DocumentEvent类在javax.swing.event包 中,其余的都在java.awt.event包 中。JComponent类在javax.swing包中,因此其子类的那些组件如JButton等也在该包中。Container类在java.awt包 中。

JFrame创建的窗口默认是不可见的。

RandomAccessFile类的seek(long a)方法,将读写位置定位于距离文件开头a个字节处

在进行使用in.read()读取源的数据,并存入到缓冲数组时,返回的int型变量是该方法读取的字符数的数量。

import java.io.*;
public class lei {
  public static void main(String args[]){
    File file = new File("hello.txt");  
    byte [] b = "abcdef".getBytes();
    try{
        FileOutputStream out = new FileOutStream(file);
        out.write(b);
        FileInputStream in = new FileInputStream(file);
        byte [] a = new byte[5];
        int m = in.read(a,0,5);
        String s1 = new String(a);
        m = in.read(a,0,5);
        System.out.printlf("%s,%d",s1,m);  //abcde,1
        
    }
    catch(Exception exp){}
  }
}

当被存入的数组a溢出后,它会被从头重新读入,并且是一个字符一个字符覆盖。

import java.io.*;
public class lei {
  public static void main(String args[]){
    File file = new File("hello.txt");  
    byte [] b = "abcdef".getBytes();
    try{
        FileOutputStream out = new FileOutStream(file);
        out.write(b);
        FileInputStream in = new FileInputStream(file);
        byte [] a = new byte[5];
        int m = in.read(a,0,5);
        String s1 = new String(a);
        m = in.read(a,0,5);
        String s2 = new String(a);
        System.out.printlf("%s,%s",s1,s2);  //abcde,fbcde
        
    }
    catch(Exception exp){}
  }
}

注意字符(字节)输入流中的read()和read(char[])的方法,其返回值差别非常大!read()方法只读一个字符,返回一个整数(可能是ascll码,若超过范围则数很大)。而read(char[])则返回读取的字符的个数。

import java.io.*;

public class lei {

    public static void main(String args[]){

       File file = new File("hello.txt");  //今天是个好日子

       try {

            FileReader in = new  FileReader(file);
            char c[]=new char[7];
            int m =in.read(c);

            System.out.println(m); //7

       }

       catch(IOException exp){}

    }

}

如果使用read()方法:

import java.io.*;

public class lei {

    public static void main(String args[]){

       File file = new File("hello.txt");  //今天是个好日子

       try {

            FileReader in = new  FileReader(file);
            //char c[]=new char[7];
            int m =in.read();

            System.out.println((char)m); //今
           //System.out.println((char)m) : 20170
       }

       catch(IOException exp){}

    }

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值