zoukankan      html  css  js  c++  java
  • 41、使用字符流解决乱码问题

    字符流FileReader

    字符流FileReader主要用来读取字符的IO流,使用字符流读取文本文件可以解决乱码问题。

    package com.sutaoyu.IO;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class IO_test_8 {
        public static void main(String[] args){
            FileReader fr = null;
            try {
                fr = new FileReader("word.txt");
                int temp;
                while((temp = fr.read()) != -1) {
                    System.out.println((char)temp);
                }
            }catch(FileNotFoundException e) {
                e.printStackTrace();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
    }

    使用缓冲流BufferedReader可以一次读取一行的文字:

    package com.monkey1024.chario;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class FileReaderTest02 {
    
        public static void main(String[] args) {
            try (BufferedReader br = new BufferedReader(new FileReader("word.txt"));) {
                String s;
                //一次读取一行
                while ((s = br.readLine()) != null) {
                    System.out.print(s);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    字符流FileWriter

    使用FileWriter可以解决写出文本文件中文乱码的问题

    package com.sutaoyu.IO;
    
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class IO_test_10 {
        public static void main(String[] args) {
            FileWriter fw = null;
            try {
                fw = new FileWriter("netword.txt");
                fw.write("我洗喜欢学习java");
                fw.write(97);
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }

    使用BufferedWriter缓冲流写出文字

    package com.sutaoyu.IO;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class IO_test_11 {
        public static void main(String[] args) {
            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new FileWriter("netword.txt"));
                bw.write("我喜欢打篮球");
                bw.newLine();//换行
                bw.write("我喜欢踢足球");
                bw.flush();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }

    使用字符流拷贝文件

    使用字符流拷贝文本文件可以避免文件中的内容乱码,需要注意的是字符流不能拷贝非文本文件,比如照片。

    package com.sutaoyu.IO;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class IO_test_12 {
        public static void main(String[] args) {
            BufferedReader br = null;
            BufferedWriter bw = null;
            try {
                bw = new BufferedWriter(new FileWriter("copyword.txt"));
                br = new BufferedReader(new FileReader("word.txt"));
                String s;
                while((s= br.readLine()) != null) {
                    bw.write(s);
                    bw.newLine();
                }
                bw.flush();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    手把手教你利用create-nuxt-app脚手架创建NuxtJS应用
    初识NuxtJS
    webpack打包Vue应用程序流程
    用选择器代替表格列的筛选功能
    Element-UI
    Spectral Bounds for Sparse PCA: Exact and Greedy Algorithms[贪婪算法选特征]
    Sparse Principal Component Analysis via Rotation and Truncation
    Generalized Power Method for Sparse Principal Component Analysis
    Sparse Principal Component Analysis via Regularized Low Rank Matrix Approximation(Adjusted Variance)
    Truncated Power Method for Sparse Eigenvalue Problems
  • 原文地址:https://www.cnblogs.com/zhuifeng-mayi/p/10143315.html
Copyright © 2011-2022 走看看