• JAVA进行基础的文件IO读写


     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 
     7 public class TestIO {
     8     
     9     public void testRead(String path){
    10         FileInputStream is = null;
    11         File file = new File(path);
    12         try {
    13             is = new FileInputStream(file);
    14             byte[] byteBuffer = new byte[is.available()];
    15             int read = 0;
    16             while((read = is.read(byteBuffer))!= -1){
    17                 System.out.write(byteBuffer, 0, read);
    18             }
    19         } catch (FileNotFoundException e) {
    20             e.printStackTrace();
    21         } catch (IOException e) {
    22             e.printStackTrace();
    23         }finally{
    24             try {
    25                 if(is!=null){
    26                     is.close();
    27                 }
    28             } catch (IOException e) {
    29                 e.printStackTrace();
    30             }
    31         }
    32     }
    33     
    34     public void testWrite(String content, String path){
    35         File file = new File(path);
    36         try {
    37             FileOutputStream os = new FileOutputStream(file);
    38             byte[] byteBuffer = content.getBytes();
    39             os.write(byteBuffer, 0, byteBuffer.length);
    40             os.flush();
    41             os.close();
    42         } catch (FileNotFoundException e) {
    43             e.printStackTrace();
    44         } catch (IOException e) {
    45             e.printStackTrace();
    46         }
    47     }
    48     
    49     public static void main(String[] args) {
    50         TestIO io = new TestIO();
    51         io.testRead("src/test.txt");
    52         io.testWrite("this is a test", "src/test.txt");
    53         //io.testRead("src/test.txt");
    54     }
    55     
    56 }

    import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;
    public class TestIO {public void testRead(String path){FileInputStream is = null;File file = new File(path);try {is = new FileInputStream(file);byte[] byteBuffer = new byte[is.available()];int read = 0;while((read = is.read(byteBuffer))!= -1){System.out.write(byteBuffer, 0, read);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {if(is!=null){is.close();}} catch (IOException e) {e.printStackTrace();}}}public void testWrite(String content, String path){File file = new File(path);try {FileOutputStream os = new FileOutputStream(file);byte[] byteBuffer = content.getBytes();os.write(byteBuffer, 0, byteBuffer.length);os.flush();os.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {TestIO io = new TestIO();io.testRead("src/test.txt");io.testWrite("this is a test", "src/test.txt");//io.testRead("src/test.txt");}}

  • 相关阅读:
    理解MapReduce计算构架
    熟悉HBase基本操作
    爬虫大作业
    熟悉常用的HDFS操作
    数据结构化与保存
    使用正则表达式,取得点击次数,函数抽离
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    课后作业-阅读任务-阅读提问-5
    课后作业-阅读任务-阅读提问-4
  • 原文地址:https://www.cnblogs.com/zhaochifan/p/5193629.html
Copyright © 2020-2023  润新知