File类 IO流 第五讲091205B

xiaoxiao2025-02-11  8

1)file.listFiles()返回f目录下的文件数组 2)File.listRoots()返回本地磁盘驱动器的数组 TestFile.java File类 import java.io.File;import java.text.SimpleDateFormat;public class TestFile { public static void main(String[] args) { File f=new File("e:\\1.txt"); System.out.println(f.exists()); System.out.println(f.canExecute()); System.out.println(f.getName()); System.out.println(f.getFreeSpace()/1024.0/1024/1024); System.out.println(f.listFiles());//(1) SimpleDateFormat smp=new SimpleDateFormat("yy-MM-dd"); System.out.println(smp.format(f.lastModified())); System.out.println(File.listRoots()[3]); //(2) }} 输出: true true 1.txt 10.352336883544922 null 09-12-06 F:\ ---------------------------------------------------------------------- MyDir.java 制作自己的DIR .对file类的属性方法进行练习。 import java.io.File;import java.text.DecimalFormat;import java.text.SimpleDateFormat;public class MyDir { public static void main(String[] args) { File f = new File("e:\\"); File fs[] = f.listFiles(); for (File file : fs) { Show(file); } } static void Show(File f) { SimpleDateFormat smp = new SimpleDateFormat("yy-MM-dd"); DecimalFormat df = new DecimalFormat("#.0000000MB"); String time = smp.format(f.lastModified()); String isdir = f.isDirectory() ? "<DIR>" : ""; String size = df.format(f.length() / 1024.0 / 1024); String name = f.getName(); System.out.println(String.format("%1$-10s%2$-8s%3$-15s%4$-20s", time , isdir, size, name)); }} 输出: 09-12-06 .0001984MB 1.log 09-12-06 .0000095MB 1.txt 09-12-06 .0009766MB 3.txt 09-12-06 .0000000MB 4.txt 09-12-06 .0000267MB 5.txt 09-12-06 .0000725MB data.txt 09-02-17 <DIR> .0000000MB game 08-10-06 <DIR> .0000000MB GHOST 09-12-06 .0000000MB log.txt 09-11-15 <DIR> .0000000MB movie 09-02-25 <DIR> .0000000MB music 09-12-06 <DIR> .0000000MB myjava 09-12-06 .0000343MB MyLog4j.properties 09-12-06 .0000715MB p.txt 09-12-06 .0000124MB pro.txt 09-04-22 .0004606MB pwk.rar 08-10-06 <DIR> .0000000MB RECYCLER 09-11-29 <DIR> .0000000MB soft_JAVA 70-01-01 <DIR> .0000000MB System Volume Information 09-02-24 .0131836MB Thumbs.db ---------------------------------------------------------------------- IOStreamTest.JAVA 对“E:\\1.TXT”的文件进行读写操作。fileinputstream.read();fileoutputstream.write() import java.io.*;public class IOStreamTest { public static void main(String[] args) { //输出流 try { OutputStream ops = new FileOutputStream("e:\\1.txt"); ops.write('B'); ops.write('M'); ops.write('W'); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //输入流 try { InputStream ipts = new FileInputStream("e:\\1.txt"); int i = 0; while (true) { i = ipts.read(); if (i == -1) break; System.out.print((char) i); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }} ---------------------------------------------------------------------- 1) 把bis读入的信息写入数组b[] 2) 把b[]数组里面的信息写入bos 3)BufferedInput(Output)Stream为处理流,在节点流外头套了一个套子。 StreamCopy.java 文件按位拷贝 import java.io.*;public class StreamCopy { public static void main(String[] args) { try { InputStream is = new FileInputStream("e:\\1.txt"); InputStream bis = new BufferedInputStream(is); //(3) OutputStream os = new FileOutputStream("e:\\3.txt"); OutputStream bos = new BufferedOutputStream(os); byte b[] = new byte[1024]; while ((bis.read(b)) != -1) {//(1) bos.write(b);//(2) } bos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }} ---------------------------------------------------------------------- TestFileReader.java。。。。。。。。。。。。。。。。。。。 ---------------------------------------------------------------------- 1)类要被读取写入,该类需要实现序列接口 2)PersonOut.java将定义的人物“new Person(9527, "唐伯虎")”,记录到“e:\\data.txt”文件中。PersonIn.java从文件中读出object类。 (transient使用此关键字标记属性,使该属性不被记录。Person 若实现Externalizable接口可以定义读取写入规则。) Object流 Person.java import java.io.Serializable;public class Person implements Serializable {//(1) private static final long serialVersionUID = 2894597671409392626L; int id; String name; @Override public String toString() { return this.id+"|"+this.name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person(int id, String name) { super(); this.id = id; this.name = name; }} PersonOut.java import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.OutputStream;public class PersonOut { public static void main(String[] args) { Person p = new Person(9527, "唐伯虎"); try { OutputStream os = new FileOutputStream("e:\\data.txt"); ObjectOutputStream oos = new ObjectOutputStream(os); oos.writeObject(p); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }} PersonIn.java import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;public class PersonIn { public static void main(String[] args) { try { InputStream is=new FileInputStream("e:\\data.txt"); ObjectInputStream ois= new ObjectInputStream(is); System.out.println(ois.readObject()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }} 输出: 9527|唐伯虎 ---------------------------------------------------------------------- Properties类是一类特殊的map集合框架 1)加载读取文件内的属性的方法。Properties.load(),properties.store() PropertiesLoadTest.java import java.util.*;import java.io.*;public class PropertiesLoadTest { public static void main(String[] args) { Properties pr = new Properties(); try { pr.load(new FileInputStream("e:\\pro.txt"));//(1) /*pro.txt * b=2(换行)a=1(换行) c=3 */ Set<?> s = pr.keySet(); for (Object object : s) { System.out.println(object + "=" + pr.get(object)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }} 输出: b=2 a=1 c=33 PropertiesSendTest.java import java.util.*;import java.io.*;public class PropertiesSendTest { public static void main(String[] args) { Properties pro= new Properties(); pro.put("122", "alue"); pro.put("120", "aluew"); pro.put("432", "aluewe"); try { pro.store(new FileOutputStream("e:\\p.txt"), "MyComments");//(1) } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }} ---------------------------------------------------------------------- Log4jDemo.java。。。。。。。。。。。。。。。。。。。 ----------------------------------------------------------------------
转载请注明原文地址: https://www.6miu.com/read-5024498.html

最新回复(0)