java 打印流 递归复制子文件子文件夹 不同编码文…
2018-06-18 02:57:12来源:未知 阅读 ()
package com.swift.jinjie; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintStream; /*从键盘输入一个文件夹路径,利用打印流将该文件夹下的所有文件(包括子文件夹)复制到D盘下temp文件夹下。*/ public class PrintAllToFile { public static void main(String[] args) throws IOException { File srcDir=new File("d:/abc"); File destDir=new File("d:/temp"); printAllFile(srcDir,destDir); System.out.println("包括子文件夹复制成功"); } private static void printAllFile(File srcDir, File destDir) throws IOException { if(!destDir.exists()) { destDir.mkdirs(); } File[] files=srcDir.listFiles(); for(File file:files) { if(file.isDirectory()) { File destDirNew=new File(destDir,file.getName()); printAllFile(file,destDirNew); }else { File destFile=new File(destDir,file.getName()); BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file)); PrintStream ps=new PrintStream(destFile); int len; byte[] buf=new byte[1024]; while((len=bis.read(buf))!=-1) { ps.write(buf, 0, len); } } } } }
不同编码文件复制到同一文件
package com.swift.jinjie; 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; /*d:/abc盘下有两个文本文件,分别为a.txt和b.txt,其中a.txt编码方式是gbk,而b.txt的编码方式是utf8。要求将使用转换流实现如下功能: 将a.txt和b.txt文件的内容复制到c.txt文件中,保证内容不乱码。*/ public class MergeTxt { public static void main(String[] args) throws IOException, FileNotFoundException { File file1=new File("d:/abc/a.txt"); File file2=new File("d:/abc/b.txt"); File destFile=new File("d:/abc/c.txt"); mergeFile(file1,file2,destFile); } private static void mergeFile(File file1, File file2,File destFile) throws IOException { InputStreamReader isr1=new InputStreamReader(new FileInputStream(file1),"gbk"); InputStreamReader isr2=new InputStreamReader(new FileInputStream(file2),"utf-8"); OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(destFile,true),"utf-8"); int len; char[] buf=new char[1024]; while((len=isr1.read(buf))!=-1) { osw.write(buf, 0, len); osw.flush(); } while((len=isr2.read(buf))!=-1) { osw.write(buf, 0, len); osw.flush(); } osw.close(); isr1.close(); isr2.close(); System.out.println("文件复制成功"); } }
序列化流反序列化流
package com.swift.jinjie; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Scanner; import com.swift.baseKnowledge.Student; /*有学生类包含学号,姓名,省份证号,Java成绩,数学成绩,英语成绩等成员变量,提供构造方法和setter和getter方法。 要求: * 学生信息及成绩保存到C盘的save.txt文件中 * 学生身份证号码不能保存到文件中。 * 程序运行时如果save.txt不存在,则 从键盘录入1个学生信息,信息录入格式如下: ***** 录入学生信息 ***** 请输入学号:9527 请输入姓名:华安 请输入身份证号:2203919831234543 请输入Java成绩:90 请输入数学成绩:80 请输入英语成绩:88 根据录入的信息创建学生对象并将学生对象保存到C盘下的save.txt文件中。 * 如果程序运行时,save.txt文件已经存在,则显示学生信息。格式如下: **** 学生基本信息 ***** 学号 姓名 省份证号 Java成绩 数学成绩 英语成绩 9527 华安 null 90 80 88*/ public class Fanxuliehua { public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { inputInfo(); } private static void inputInfo() throws FileNotFoundException, IOException, ClassNotFoundException { Scanner scan=new Scanner(System.in); for(int i=0;i<1;i++) { System.out.print("请输入学号:"); String id=scan.next(); Student student=new Student(); student.setId(id); System.out.println("请输入姓名:"); String name=scan.next(); student.setName(name); System.out.println("请输入身份证号:"); String uid=scan.next(); student.setUid(uid); ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("save.txt")); ObjectInputStream ois=new ObjectInputStream(new FileInputStream("save.txt")); oos.writeObject(student); Student stu=(Student)ois.readObject(); System.out.println(stu); System.out.println("学号 姓名 身份证号 数学成绩 语文成绩 总分"); System.out.println(stu.getId()+" "+stu.getName()+" "+stu.getUid()+" "+stu.getShuxue()+" "); } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:JVM 指令
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
