io文件基本操作

package com.huawei.wang;

import java.io.*;

/**
 * io流操作
 * 如果需要在文件后面追加
 * 用FileWriter,BufferWriter,RamdomAccessFile操作文件
 */
public class fileTest {

    public static void main(String[] args) {
        try{
            test5();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private static void test() throws IOException {
        File file = new File("C:\\Users\\wwx562990\\Desktop\\create or replace procedure P_C_SSOC_FINDUNITATTR");
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            //FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\wwx562990\\test.txt");
            int size = fileInputStream.available();
            byte[] bytes = new byte[size];
            fileInputStream.read(bytes);
            System.out.println(new String(bytes));

            /*int i;
            while(( i=fileInputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes);
                fileOutputStream.flush();
            }*/
            System.out.println();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }



    private static void test2() throws IOException {
        File file = new File("C:\\Users\\wwx562990\\Desktop\\create or replace procedure P_C_SSOC_FINDUNITATTR");
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\wwx562990\\Desktop\\test12.sql");
            byte[] bytes = new byte[1024];
            int i;
            while(( i=fileInputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,i);
            }
            System.out.println(new String(bytes));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }



    private static void test3() throws IOException {
        File file = new File("C:\\Users\\wwx562990\\Desktop\\123","svgdfgdf");
        File file1 = new File( file  , "qw.txt" ) ;
        if(!file.exists()){
            file.mkdirs();
        }

        file1.createNewFile();
        System.out.println(file1.getPath());

    }


    private static void test4() throws IOException {
        File File = new File("C:\\Users\\wwx562990\\Desktop\\123\\svgdfgdf");

        File oldFile = new File(File,"qw.txt");
        File newFile = new File(File,"fasf.sql");
        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;

        OutputStream outputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        inputStream = new FileInputStream(oldFile);
        bufferedInputStream = new BufferedInputStream(inputStream);

        outputStream = new FileOutputStream(newFile);
        bufferedOutputStream = new BufferedOutputStream(outputStream);

        byte[] b = new byte[1024];   //代表一次最多读取1KB的内容

        int length = 0; //代表实际读取的字节数
        while ((length = bufferedInputStream.read(b)) != -1) {
            //length 代表实际读取的字节数
            bufferedOutputStream.write(b,0,length);
        }
        //缓冲区的内容写入到文件
        bufferedOutputStream.flush();

    }

    private static void test5() throws Exception{
        String mes = "你好,world" ;
        byte[] b = mes.getBytes("UTF-8");

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream() ;
        try {
            byteArrayOutputStream.write( b );

            FileOutputStream fileOutputStream = new FileOutputStream( new File( "C:\\Users\\wwx562990\\Desktop\\123\\svgdfgdf\\qw.txt" ) ) ;
            byteArrayOutputStream.writeTo( fileOutputStream ) ;

            fileOutputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值