文件读入与输出及英文大小写转换

英文大小写转换Demo.解决文件读入与输出问题乱码问题

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ToLower {
    public static final String path="F:\\ytk\\UPTOLOWER\\1.txt";
    public static final String topath="F:\\ytk\\UPTOLOWER\\2.txt";
    public static void main(String[] args){
        boolean isComp=toLower(path,topath);
//        boolean isComp=toUpper(path,topath);
        System.out.println("文件转换状态--->"+isComp);
    }
    
    /*
     * tolower
     * */
    private static boolean toLower(String path,String topath) {
        BufferedReader bufferedReader=null;
        BufferedWriter bufferedWriter=null;
        try {
            File file=new File(path);
            if(!file.exists()) {
                System.out.println("文件不存在《《《《《《"+path);
                return false;
            }
            File toFile=new File(topath);
            if(toFile.exists()) {
                toFile.delete();
            }
            toFile.createNewFile();
            bufferedReader=
                    new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream(file),
                                    "GBK"));
            bufferedWriter=
                    new BufferedWriter(
                            new OutputStreamWriter(
                                    new FileOutputStream(toFile,true), "GBK"));
            int count=0;
            StringBuffer bufferTemp=new StringBuffer();
            String str="";
            String temp="";
            String tempTo="";
            while((str=bufferedReader.readLine())!=null) {
                count++;
                temp=str.replaceAll("\\s*","");
                tempTo=temp.toLowerCase();
                if(!checkLength(temp,tempTo)) {
                    System.out.println("第"+count+"行出现错误《《《《《《《"+temp);
                }
                temp=count+"\t\t"+tempTo+"\r\n\r\n";
                bufferTemp.append(temp);
                
            }
            bufferedWriter.write(bufferTemp.toString());
            bufferedWriter.flush();
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if(bufferedReader!=null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
            if(bufferedWriter!=null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return true;
    }
    
    /*
     * toUpper
     * */
    private static boolean toUpper(String path,String topath) {
        BufferedReader bufferedReader=null;
        BufferedWriter bufferedWriter=null;
        try {
            File file=new File(path);
            if(!file.exists()) {
                System.out.println("文件不存在《《《《《《"+path);
                return false;
            }
            File toFile=new File(topath);
            if(toFile.exists()) {
                toFile.delete();
            }
            toFile.createNewFile();
            bufferedReader=
                    new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream(file),
                                    "GBK"));
            bufferedWriter=
                    new BufferedWriter(
                            new OutputStreamWriter(
                                    new FileOutputStream(toFile,true), "GBK"));
            int count=0;
            StringBuffer bufferTemp=new StringBuffer();
            String str="";
            String temp="";
            String tempTo="";
            while((str=bufferedReader.readLine())!=null) {
                count++;
                temp=str.replaceAll("\\s*","");
                tempTo=temp.toUpperCase();
                if(!checkLength(temp,tempTo)) {
                    System.out.println("第"+count+"行出现错误《《《《《《《"+temp);
                }
                temp=count+"\t\t"+tempTo+"\r\n\r\n";
                bufferTemp.append(temp);
                
            }
            bufferedWriter.write(bufferTemp.toString());
            bufferedWriter.flush();
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if(bufferedReader!=null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
            if(bufferedWriter!=null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return true;
    }
    
    /*
     * checklength between str1 and str2
     * */
    private static boolean checkLength(String str1,String str2){
        if(str1.length()==str2.length())
            return true;
        else
            return false;
    }
    
}



猜你喜欢

转载自blog.csdn.net/mister_yang/article/details/79602172