第一种:.read() 一次读一个字节,返回值类型是int,方法读取硬盘访问次数太频繁。缺点:效率低,伤硬盘
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class FileInputStreamTest { public static void main(String[] args) { // TODO Auto-generated method stub FileInputStream fis = null; try { fis = new FileInputStream("C:\\update.txt"); int temp; while((temp=fis.read())!=-1) { System.out.print(temp); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
执行结果:
678269658469327980847377739069756989321211019711432797832846566766932991061021001161161310131073788369828432737884793284656676693299106102100116116328773847232756989326132121101971141310973787073766932349958929910610210048504611612011634131013106882798032798084737773906975698932121101971143279783284656676693299106102100116116
第二种:使用.read(byte[] bytes) 一次读取多个字节。该方法执行结束后,返回值是该次读到的字节数,读不到则返回-1
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class FileInputStreamTest01 { public static void main(String[] args) { // TODO Auto-generated method stub FileInputStream fis = null; try { fis = new FileInputStream("C:\\update.txt"); byte[] bs = new byte[10]; int temp; while((temp=fis.read(bs))!=-1) { // System.out.println(temp); String s = new String(bs,0,temp); //把数组中的元素全部转换成字符串打印出来 System.out.print(s); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
执行结果:
CREATE OPTIMIZEKEY year ON TABLE cjfdtt INSERT INTO TABLE cjfdtt WITH KEY = year INFILE "c:\cjfd02.txt" DROP OPTIMIZEKEY year ON TABLE cjfdtt