Java输入流的学习例子



public class FileIO {
 /**
  * 路径分隔符
  */

 public static void test1() {
  System.out.println(File.pathSeparator);
  System.out.println(File.separator);
  
 }
 /**
  * 创建文件
  */
 public static void test2() {
  File file = new File("readme.txt");//创建名为readme的txt文件
  try {
   file.createNewFile();
  } catch (IOException e) {
   
   e.printStackTrace();
  }
 }
 /**
  * 字节流键盘输入
  */

 public static void test3() throws IOException {
  InputStream is = System.in;
  int a;
  try {
   while ((a=is.read())!=-1) {
    char b =(char) a;
    System.out.print(b);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   try {
    is.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 /**
  * 字符输入流:BufferedReader(自带缓冲区)
  */

 public static void test4() {
  BufferedReader br= null;
  try {
   br = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
   while (br.ready()) {
    System.out.println(br.readLine());
    
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   try {
    br.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 /**
  * 字符输入流:InputStreamReader
  */

 public static void test5() {
  InputStreamReader is = null;
  try {
   is = new InputStreamReader(new FileInputStream("a.txt"));
   int a;
   while((a = is.read())!=-1)
   System.out.print((char)a);

  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    is.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 /**
  * 字节输入流:FileInputStream
  */

 public static void test6() {
  InputStream is = null;
  try {
   is = new FileInputStream("a.txt");
   int a;
   while ((a = is.read())!=-1) {
    char b =(char) a;
    System.out.print(b);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   try {
    is.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
 }
 /**
  * 文件增加可执行权
  *
  */

 public static void test7() {
  File file = new File("a.txt");
  file.setExecutable(true);
  System.out.println(file.canExecute());
 }
 /**
  *文件重命名
  */

 public static void test8() {
  File file = new File("readme.txt");
  file.renameTo(new File("b.txt"));
 }
 /**
  * 查看文件权限
  */

 public static void test9() {
  File file = new File("a.txt");
  System.out.println(file.canExecute()+","+file.canRead()+","+file.canWrite());
 }
 /**
  * 文件权限操作
  */

 public static void test10() {
  File file = new File("a.txt");
  file.setExecutable(true,false);
  System.out.println(file.canExecute()+","+file.canRead()+","+file.canWrite());//查看执行权,读取权,写入权
 }
 
 /**
  * 增强For
  */

 public static void test11() {
  
   String a = "a";
   byte[] b = a.getBytes();
   System.out.println(b.length);
   for (byte i : b) {
    System.out.println(i); 
  }

 }
 /**
  * 功能:演示一下ByteArrayInputStream
  * *将数组通过字节流ByteArrayInputStream读进内存
  */

 public static void test12() {
  byte[] b = "hello".getBytes();
  InputStream is = new ByteArrayInputStream(b);
  int a;
  try {
   while ((a = is.read()) != -1) {
    char c = (char) a;
    System.out.print(c);
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    is.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 /**
  * 字节输入流:BufferedInputStream(自带缓冲区)
  */

 public static void test13() {
  BufferedInputStream bs = null;
  try {
   bs = new BufferedInputStream(new FileInputStream("a.txt"));
   byte[] byteArray = new byte[20];
   try {
    while ((bs.read(byteArray)) != -1) {
     System.out.println(new String(byteArray));
    }
   } catch (IOException e) {
    e.printStackTrace();
   }

  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } finally {
   try {
    bs.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值