package IO;
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class ArrayListToTextDemo {
public static void main(String[] args) throws IOException {
ArrayList<String > arrlist=new ArrayList<String>();
arrlist.add("hello");
arrlist.add("world");
arrlist.add("java");
BufferedWriter bw=new BufferedWriter(new FileWriter("ieda_test\\src\\IO\\fos.txt"));
for (String s:arrlist){
bw.write(s);
bw.newLine();
}
bw.close();
}
}
执行结果:
public class TextToArrayListDemo {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("ieda_test\\src\\IO\\fos.txt"));
ArrayList<String> arrayList=new ArrayList<String>();
String len;
while ((len=br.readLine())!=null){
arrayList.add((String) len);
}
br.close();
for (String s:arrayList){
System.out.println(s);
}
}
}
执行结果:
public class CallNameDemo {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("ieda_test\\src\\IO\\fos.txt"));
ArrayList<String > arrayList=new ArrayList<String>();
String s;
while((s=br.readLine())!=null){
arrayList.add(s);
}
br.close();
Random r=new Random();
int randNum=r.nextInt(arrayList.size());
System.out.println(arrayList.get(randNum));
}
}
package IO;
import java.io.*;
import java.util.ArrayList;
public class arrayListToTextDemoPlus {
public static void main(String[] args) throws IOException {
Student student1=new Student("itheima001","林青霞","西安");
Student student2=new Student("itheima002","张曼玉","罗马");
Student student3=new Student("itheima003","尹志平","长安");
ArrayList<Student> arrayList=new ArrayList<Student>();
arrayList.add(student1);
arrayList.add(student2);
arrayList.add(student3);
BufferedWriter bw=new BufferedWriter(new FileWriter("ieda_test\\src\\IO\\fos.txt"));
for (Student s:arrayList){
bw.write(s.getId()+" "+s.getName()+" "+s.getAddress());
bw.newLine();
bw.flush();
}
bw.close();
}
}
执行结果:
在fox.txt中
package IO;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.TreeSet;
public class arrayListToTextDemoSortEdition {
public static void main(String[] args) throws IOException {
StudentNew sn1 = new StudentNew("林青霞", 98, 85, 99);
StudentNew sn2 = new StudentNew("尹志平", 100, 55, 94);
StudentNew sn3 = new StudentNew("小龙女", 99, 95, 69);
StudentNew sn4 = new StudentNew("杨过", 88, 100, 78);
StudentNew sn5 = new StudentNew("古天乐", 99, 95, 98);
TreeSet<StudentNew> ts = new TreeSet<StudentNew>(new Comparator<StudentNew>() {
@Override
public int compare(StudentNew o1, StudentNew o2) {
//比总成绩
int num1 = o1.getSum() - o2.getSum();
//次要条件,总分相同的时候,按照语文,数学,英语成绩大小排序
int num2 = num1 == 0 ? o1.getChinese() - o2.getChinese() : num1;//比较语文
int num3 = num2 == 0 ? o1.getMath() - o2.getMath() : num2;//比较数学,如果数学英语都一样那么英语肯定也一样了
//如果每一科成绩都相同,还需比较名字是否相同
int num4 = num3 == 0 ? o1.getName().compareTo(o2.getName()) : num3;
return num4;
}
});
ts.add(sn1);
ts.add(sn2);
ts.add(sn3);
ts.add(sn4);
ts.add(sn5);
BufferedWriter bw = new BufferedWriter(new FileWriter("ieda_test\\src\\IO\\fos.txt"));
for (StudentNew s : ts) {
bw.write(s.getName() + " 语文成绩=" + s.getChinese() + " 数学成绩=" + s.getMath() + " 英语成绩=" + s.getEnglish()+"总分="+s.getSum());
bw.newLine();
bw.flush();
}
bw.close();
}
}
执行结果:
在fos.txt中
package IO;
import java.io.*;
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
//创建数据源目录File对象,路径是D:\litcast
File srcFolder = new File("D:\\itcast");
//获取数据源目录File对象的名称(itcast)
String srcFolderName = srcFolder.getName();//itcast
//目的地目录file对象
File destFolder = new File("ieda_test", srcFolderName);
//判断目的地目录是否存在,不存在则创建
if (!destFolder.exists()) {
destFolder.mkdir();
}
//获取该源目录下所有文件数组
File[] listFiles = srcFolder.listFiles();
//遍历File数组
for (File srcFile : listFiles) {
String srcFileName = srcFile.getName();
//创建目的地文件File对象,路径名是目的地目录+文件名 组成(myCharStream\ \itcast\ \mn. jpg)
File destFile = new File(destFolder, srcFileName);
//复制文件
copyFile(srcFile, destFile);
}
}
private static void copyFile(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bis.close();
bos.close();
}
}
运行结果:
复制单级文件成功
package IO;
import java.io.*;
public class CopyMultiFolderDemo {
public static void main(String[] args) throws IOException {
File srcFile = new File("D:\\itcast");
String srcFolderName = srcFile.getName();
//目的地目录file对象
File destFolder = new File("ieda_test", srcFolderName);
//判断目的地目录是否存在,不存在则创建
if (!destFolder.exists()) {
destFolder.mkdir();
}
Copy(srcFile,destFolder);
}
//srcFile为源文件夹,destFolder是目标文件夹
private static void Copy(File srcFile, File destFolder) throws IOException {
File[] fileArray=srcFile.listFiles();
for (File f:fileArray){
if(f.isFile()){
File newFile=new File(destFolder,f.getName());
copyFile(f,newFile);
}
else if (f.isDirectory()){
File makedir=new File(destFolder,f.getName());
if (!makedir.exists()) {
makedir.mkdir();
}
Copy(f,makedir);
}
}
}
//复制文件
private static void copyFile(File srcFile, File destFile) throws IOException {
//字节流复制,一次复制1024个字节数组长度
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bis.close();
bos.close();
}
}
执行结果:
复制源文件:
复制结果:
复制成功!
①直接抛出异常
//①直接抛出异常
public static void method1() throws IOException {
FileWriter fw=new FileWriter("ieda_test\\Copy.java");
FileReader fr=new FileReader("ieda_test\\src\\IO\\IODemo01.java");
int len;
char[] chs=new char[1024];
while ((len=fr.read(chs))!=-1){
fw.write(chs,0,len);
}
fw.close();
fr.close();
}
② 最常见的try catch finally写法
//② 最常见的try catch finally写法
public static void method2(){
FileWriter fw=null;
FileReader fr=null;
try {
fw=new FileWriter("ieda_test\\Copy.java");
fr=new FileReader("ieda_test\\src\\IO\\IODemo01.java");
int len;
char[] chs=new char[1024];
while ((len=fr.read(chs))!=-1){
fw.write(chs,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if (fw!=null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr!=null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这里主要最麻烦的是finally那里去进行资源的释放,fr与fw不是在finally定义的无法直接获取到,所以又需要别的一系列操作。
③ JDK7之后的改进方案
//③ JDK7之后的改进方案
public static void method3(){
try(FileWriter fw=new FileWriter("ieda_test\\Copy.java");
FileReader fr=new FileReader("ieda_test\\src\\IO\\IODemo01.java");) {
int len;
char[] chs=new char[1024];
while ((len=fr.read(chs))!=-1){
fw.write(chs,0,len);
}
}catch (IOException e){
e.printStackTrace();
}
}
简化了资源的释放,在try中的IO流对象会自动close。
也就是说,ObjectOutputStream可以将对象进行写入文件中,ObjectInputStream可以将文件中的对象重新序列化获取到Java中。但是将对象序列化写入的那个对象必须实现Serializable接口。
public class ObjectInputStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("ieda_test\\src\\IO\\fos.txt"));
Object obj=ois.readObject();
Student s=(Student) obj;
System.out.println(s.getName());
ois.close();
}
}
执行结果:
需要注意一种情况:
就是如果将对象写入到文件中了之后,再对此对象进行修改,例如增加了一个toString方法,此时再去读取这个对象就会报错了。
原因是:
每次在对 对象进行序列化Serializable的时候,都会生成一个serialVersionUID,所以写入文件的时候的那个serialVersionUID和修改对象之后的serialVersionUID是不同的话,那么就会报错了。
解决方案:
给定一个序列化serialVersionUID值不变。
private static final long serialVersionUID= xxxx L ;
(这个L是long的意思)
public class Student implements Serializable {
private static final long serialVersionUID= 77L ;
private String id;
private String name;
private String address;
}