指定编码格式读写文件和读取文件夹下所有文件

来源:互联网 发布:票据打印的软件 编辑:程序博客网 时间:2024/06/05 16:18
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import org.junit.Test;public class ReadWriteFileWithEncode {@Testpublic void gbkToUtf() throws Exception, Exception{String path = "d:/src";//readfile(path);deletefile(path);}/**     * 读取某个文件夹下的所有文件     */    public static boolean readfile(String filepath) throws FileNotFoundException, IOException {            try {                    File file = new File(filepath);                    if (!file.isDirectory()) {                            System.out.println("文件");                            System.out.println("path=" + file.getPath());                            System.out.println("absolutepath=" + file.getAbsolutePath());                            System.out.println("name=" + file.getName());                    } else if (file.isDirectory()) {                            System.out.println("文件夹");                            String[] filelist = file.list();                            for (int i = 0; i < filelist.length; i++) {                                    File readfile = new File(filepath + "\\" + filelist[i]);                                    if (!readfile.isDirectory()) {                                            System.out.println("path=" + readfile.getPath());                                            System.out.println("absolutepath="                                                            + readfile.getAbsolutePath());                                            System.out.println("name=" + readfile.getName());                                            if(/*readfile.getName().endsWith(".java")*/true){                                            String content = ReadWriteFileWithEncode.read(readfile.getPath(), "gbk");                                            System.out.println("Content------------>"+content);                                            String path = readfile.getPath();                                            if(path.contains(":")){                                            path="E"+path.substring(1);                                            System.out.println("path------->"+path);                                            }                                            ReadWriteFileWithEncode.write(path,content,  "utf-8" );                                            }                                    } else if (readfile.isDirectory()) {                                            readfile(filepath + "\\" + filelist[i]);                                    }                            }                    }            } catch (FileNotFoundException e) {                    System.out.println("readfile()   Exception:" + e.getMessage());            }            return true;    }    /**     * 删除某个文件夹下的所有文件     */    @Test    public static boolean deletefile(String filepath) throws FileNotFoundException, IOException {            try {                    File file = new File(filepath);                    if (!file.isDirectory()) {                            System.out.println("文件");                            System.out.println("path=" + file.getPath());                            System.out.println("absolutepath=" + file.getAbsolutePath());                            System.out.println("name=" + file.getName());                    } else if (file.isDirectory()) {                            System.out.println("文件夹");                            String[] filelist = file.list();                            for (int i = 0; i < filelist.length; i++) {                                    File readfile = new File(filepath + "\\" + filelist[i]);                                    if (!readfile.isDirectory()) {                                            System.out.println("path=" + readfile.getPath());                                            System.out.println("absolutepath="                                                            + readfile.getAbsolutePath());                                            System.out.println("name=" + readfile.getName());                                            String path = readfile.getPath();                                        if(path.contains(":")){                                        path="E"+path.substring(1);                                        System.out.println("path------->"+path);                                        }                                        File file1 = new File(path);                                            System.out.println(file1.delete());//                                            if(readfile.getName().endsWith(".java")){//                                            String content = ReadWriteFileWithEncode.read(readfile.getPath(), "gbk");//                                            String path = readfile.getPath();//                                            if(path.contains(":")){//                                            path="E"+path.substring(1);//                                            System.out.println("path------->"+path);//                                            }//                                            ReadWriteFileWithEncode.write(path,content,  "utf-8" );//                                            }                                    } else if (readfile.isDirectory()) {                                            readfile(filepath + "\\" + filelist[i]);                                    }                            }                    }            } catch (FileNotFoundException e) {                    System.out.println("readfile()   Exception:" + e.getMessage());            }            return true;    }/** * 按指定格式写指定内容到指定文件中 * @param path * @param content * @param encoding * @throws IOException */public static void write(String path, String content, String encoding)throws IOException {File file = new File(path);file.delete();file.createNewFile();BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));writer.write(content);writer.close();}/** * 按指定格式读指定文件中的内容 * @param path * @param encoding * @return * @throws IOException */public static String read(String path, String encoding) throws IOException {String content = "";File file = new File(path);BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));String line = null;while ((line = reader.readLine()) != null) {content += line + "\n";}reader.close();return content;}public static void main(String[] args) throws IOException {//String content = "中文内容";//String path = "c:/test.txt";//String encoding = "utf-8";//ReadWriteFileWithEncode.write(path, content, encoding);//System.out.println(ReadWriteFileWithEncode.read(path, encoding));String path = "d:/src";readfile(path);//deletefile(path);}}    

0 0
原创粉丝点击