Java 初学者-javaIO4
今天学习了什么?
今天继续学习了javaIO操作有关知识。
编写了一些代码,了解了FileInputStream类
FileInputStream
import java.io.*; public class FileInputStreamTest01 { public static void main(String[] args) { // TODO Auto-generated method stub FileInputStream fis=null; try { //创建文件输入流对象 //文件路径,绝对路径 “\\”或“/” fis=new FileInputStream("E:\\java数据文件\\a.TXT"); int read1=fis.read(); char m=(char)read1;//括号不能反 System.out.println(m); System.out.println((char)read1);//一个一个字节读入缓冲区,如果没有更多的数据,因为文件的结尾已经到达, 返回-1 System.out.println(read1); }catch(FileNotFoundException e) { e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); }finally { if(fis!=null) { try { fis.close();//会抛出IOException异常,因此需要处理 }catch(IOException e) { e.printStackTrace(); } } } } }

import java.io.*; public class FileInputStream02 { public static void main(String[] args) { // TODO Auto-generated method stub // TODO Auto-generated method stub FileInputStream fis=null; try { //创建文件输入流对象 //文件路径,绝对路径 “\\”或“/” fis=new FileInputStream("E:\\java数据文件\\a.TXT"); /* while(true) { int readda=fis.read(); if(readda==-1) { break; } System.out.println(readda); }*/ int read=0; while((read=fis.read())!=-1) { System.out.println(read); } }catch(FileNotFoundException e) { e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); }finally { if(fis!=null) { try { fis.close();//会抛出IOException异常,因此需要处理 }catch(IOException e) { e.printStackTrace(); } } } } }

import java.io.*; public class FileInputStreamTestbyte03 { public static void main(String[] args) { // TODO Auto-generated method stub FileInputStream fis=null; //文件内容1decdef try { fis=new FileInputStream("E:\\java数据文件\\a.TXT"); //利用byte数组读数据 byte[] bytes=new byte[4]; int read=fis.read(bytes);//返回到的是字节数量 System.out.println(read); // System.out.println(new String(bytes)); System.out.println(new String(bytes,0,read)); read=fis.read(bytes);//第二次读长度不够,因此返回2,并且覆盖数组中前三个字节 // System.out.println(new String(bytes));//defc,果然被覆盖 System.out.println(new String(bytes,0,read));//输出指定长度的字符串 System.out.println(read); }catch(FileNotFoundException e) { e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); }finally { if(fis!=null) { try { fis.close(); }catch(IOException e) { e.printStackTrace(); } } } } }
明天计划学习什么?
明天计划学习IO.