读取 写入 复制 文件 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class text6 {public static void main(String[] args) {// System.out.println(fiel_read("file\\a.txt"));
//
// String str="abcdefg123456";
// file_write("file2","/a.txt",str);System.out.println(file_copy("file\\2.md","file\\1.md"));}public static boolean file_copy(String file,String copyfile){ //复制文件try (FileOutputStream fos=new FileOutputStream(file); //复制文件的路径FileInputStream fis=new FileInputStream(copyfile); //被复制文件路径){while (true){ //读取所有内容
// int n=fis.read();
// if(n==-1){break;}
// fos.write(n);byte[] bs = new byte[1024];int n = fis.read(bs);if (n == -1) {break;}fos.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 (FileInputStream fis = new FileInputStream(dirPath);) {while (true) {//利用read(byte[])+循环读取文件byte[] ss = new byte[1024];int n = fis.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 (FileOutputStream fos=new FileOutputStream(dirPath+name,true);){fos.write(data.getBytes());//fos.close();}catch (Exception e){System.out.println(e.toString());}}}