- 读取 写入 复制 文件
-
- import java.io.*;
-
- public class text6 {
- public static void main(String[] args) {
-
- long s1=System.nanoTime();
- // System.out.println(fiel_read("file\\a.txt")); //读取文件
- //
- // String str="abcdefg123456你好中国";
- // file_write("file2","/a.txt",str); //写入文件
- //
- // System.out.println(file_copy("file\\1.md","file\\2.md")); //复制文件 原有文件 新文件
-
-
-
- //下面几个方法好用 已经封装好了
- String str="abcdefg123456你好中国";
- //file_write2("file","/e.txt",str); //写入文件
- file_write_coded("file","/m.txt",str,"GBK"); //带编码格式写入
- System.out.println(fiel_read_coded("file/m.txt","GBK")); //带编码读取
- System.out.println(fiel_read2("file/m.txt")); //读取
-
-
- long s2=System.nanoTime();
- System.out.println("耗时"+(s1-s2)/1E9+"秒");
-
- }
-
- public static String fiel_read2(String dirPath) { //读取
- StringBuffer retstr = new StringBuffer();
- try (
- BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(dirPath))); //dirPath
- ){
- while (true) {
- String s = br.readLine();
- if (s == null) {break;}
- retstr.append(s);
- }
- }catch (Exception e){
- System.out.println(e.toString());
- }
- return retstr.toString();
- }
-
- public static String fiel_read_coded(String dirPath,String coded) { //带编码读取
- if(coded==""){coded="UTF-8";}
- StringBuffer retstr = new StringBuffer();
- try (
- BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(dirPath),coded));
- ){
- while (true) {
- String s = br.readLine();
- if (s == null) {break;}
- retstr.append(s);
- }
- }catch (Exception e){
- System.out.println(e.toString());
- }
- return retstr.toString();
- }
-
- public static void file_write_coded(String dirPath,String name,String data,String coded) { //写入文件带编码写入
- if(coded==""){coded="UTF-8";}
- File file = new File(dirPath);
- if (!file.exists() && !file.isDirectory()) {file.mkdirs();}
- try (
- FileOutputStream fos=new FileOutputStream(dirPath+name,true);
- OutputStreamWriter osw=new OutputStreamWriter(fos,coded);
- PrintWriter pw=new PrintWriter(osw);
- ){
- pw.write(data);
- }catch (Exception e){
- System.out.println(e.toString());
- }
- }
-
-
- public static void file_write2(String dirPath,String name,String data) { //写入文件
- File file = new File(dirPath);
- if (!file.exists() && !file.isDirectory()) {file.mkdirs();}
- try (
- PrintWriter pw=new PrintWriter(new FileWriter(dirPath+name,true));
- ){
- // pw.print(5.5);
- // pw.println("一二三四五六");
- // pw.write("你好在吗?");
- pw.write(data);
- }catch (Exception e){
- System.out.println(e.toString());
- }
- }
-
-
-
-
-
-
- public static boolean file_copy(String file,String copyfile){ //复制文件 原有文件 新文件
- try (
- BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(copyfile)); //缓冲数据流 复制文件的路径
- BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file)); //缓冲数据流 被复制文件路径
- ){
- while (true){ //读取所有内容
- // int n=fis.read();
- // if(n==-1){break;}
- // fos.write(n);
- byte[] bs = new byte[1024];
- int n = bis.read(bs);
- if (n == -1) {break;}
- bos.write(bs);//将数组中的数据写入到目标文件
- }
- return true;
- }catch (Exception e){
- System.out.println(e.toString());
- }
- return false;
- }
-
- public static String fiel_read(String dirPath) { //读取文件
- StringBuffer retstr = new StringBuffer();
- try (BufferedInputStream bis=new BufferedInputStream(new FileInputStream(dirPath)); //缓冲数据流
- ) {
- while (true) {//利用read(byte[])+循环读取文件
- byte[] ss = new byte[1024];
- int n = bis.read(ss);
- if (n == -1) {
- break;
- }
- for (int i = 0; i < n; i++) {
- //System.out.println((char)ss[i]);
- retstr.append((char) ss[i]);
- }
- }
- //fis.close();
- } catch (Exception e) {
- System.out.println(e.toString());
- }
- return retstr.toString();
- }
-
- public static void file_write(String dirPath,String name,String data) { //写入文件
- File file = new File(dirPath);
- if (!file.exists() && !file.isDirectory()) {file.mkdirs();}
-
- try (BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dirPath+name,true)); //缓冲数据流
- ){
- bos.write(data.getBytes());
- //fos.close();
- }catch (Exception e){
- System.out.println(e.toString());
- }
- }
-
- }