以各种方式读写文件

public class ReadFormFile
{
    // 以二进制方式读取文件 常用于读取二进制文件 图片 声音 视频等
    public static void readByBytes(String fileName)
    {
        File file = new File(fileName);
        InputStream in = null;
        try
        {
            System.out.println("一次读取一个字节");
            in = new FileInputStream(file);
            int temp;// temp存放的是编码表对应的字符编码
            while ((temp = in.read()) != -1)
            {
                System.out.println(temp);
                // System.out.write(temp);
            }
            // 文件末尾输出-1
            in.close();
        } catch (IOException e)
        {
            e.printStackTrace();
            return;
        }
 
    }
 
    public static void readByteMore(String fileName)
    {
        InputStream in = null;
        File file = new File(fileName);
        try
        {
            System.out.println("一次读取多个字节");
            byte[] temp = new byte[1024];
            int buffer = 0;
            in = new FileInputStream(fileName);
            while ((buffer = in.read(temp)) != -1)
            {
                // System.out.println(buffer );
                System.out.write(temp, 0, buffer);
            }
            System.out.println(buffer);
            in.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//以字符方式读取文件
    public static void readByChar(String fileName)
    {
        File file = new File(fileName);
        InputStreamReader reader = null;
        try
        {
            reader = new InputStreamReader(new FileInputStream(file));
            int temp = 0;
            while ((temp = reader.read()) != -1)
            {
                System.out.print((char) temp);
            }
            reader.close();
        } catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
 
    public static void readByCharMore(String fileName)
    {
        File file = new File(fileName);
        InputStreamReader reader = null;
        try
        {
            reader = new InputStreamReader(new FileInputStream(file));
            int charLength = 0;
            char[] temp = new char[30];
            while ((charLength = reader.read(temp)) != -1)
            {
                System.out.println(temp);
 
            }
        } catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
 
    public static void writeFileByByte(String fileName)// 以字节为单位写文件
    {
        File file = new File(fileName);
        OutputStream out = null;
        try
        {
            String wenjian = "haha";
            out = new FileOutputStream(file);
            Scanner scanner = new Scanner(System.in);
            byte[] bytes = null;
            while (!wenjian.equals("@"))
            {
                wenjian = scanner.next();
                bytes = wenjian.getBytes();// 将字符串转换成字节数组
                out.write(bytes);
            }
            out.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
 
    public static void writeByChar(String fileName)//以字符为单位写入字符串
    {
        File file = new File(fileName);
        OutputStreamWriter outputStreamWriter = null;
        try
        {
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file));
            String string = "cui meng meng sha gua";
            outputStreamWriter.write(string);
            outputStreamWriter.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static void writeByLine(String fileName)//格式化写入文件   每次写入一行
    {
        File file=new File(fileName);
        PrintWriter writer=null;
        try
        {
            writer=new PrintWriter(new FileOutputStream(file));//通常用于格式化读入
            writer.println(123);//此时的123是int型的
            writer.println("cui meng meng sha gua");//字符串
            writer.flush();//将流中的字节刷新到文件中
            writer.close();
        } catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    public static void main(String[] args)
    {
        String fileName = "C:/Users/Administrator/Desktop/yyy.txt";
        // ReadFormFile.readByBytes(fileName);
        // ReadFormFile.readByteMore(fileName);
        // ReadFormFile.readByChar(fileName);
        // ReadFormFile.readByCharMore(fileName);
        // ReadFormFile.writeFileByByte(fileName);
        //ReadFormFile.writeByChar(fileName);
        ReadFormFile.writeByLine(fileName);
    }
}
posted @ 2016-07-27 10:12  -梦里不知身是客  阅读(193)  评论(0编辑  收藏  举报