黑马程序员---2015.6.26java基础笔记--File类--Properties--PrintStream--SequenceInputStream--RandomAccessFile
来源:互联网 发布:淘宝买家数据采集 编辑:程序博客网 时间:2024/06/05 17:58
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
1.
import java.io.*;class FileDemo2{ public static void main(String[] args)throws IOException { //listFiles() ; test(); } //File对象必须是封装了一个目录,该目录必须存在 public static void ListDemo() { //如果文件d.txt下面没有其他文件,则输出空 File f = new File("f:\\d.txt"); String[] lists = f.list(); for(String s:lists) { sop(s); } } //列出所有盘符 public static void listRoots() { File[] f1 = File.listRoots(); for(File f:f1) { sop(f); } } //过滤出指定格式的文件 public static void list() { File f = new File("c:\\"); String[] arr = f.list(new FilenameFilter(){ public boolean accept(File dir, String name) { //sop("dir:"+dir+" name:"+name); return name.endsWith(".java"); } }); sop("length:"+arr.length); for(String s:arr) { sop(s); } } //返回指定路径名目录下的文件对象 public static void listFiles() { File f = new File("F:\\java系列教程视频\\黑马教学视频"); File[] files = f.listFiles(); for(File ff:files) { sop("名称:"+ff.getName()); } } public static void sop(Object obj) { System.out.println(obj); }}
2.
/*列出指定目录下文件或者文件夹,包含子目录的内容。因为目录中还有目录,只要使用同一个列出目录功能的函数完成即可。在列出过程中出现的还是目录的话,还可以再次调用本功能。也就是函数自身调用自身。这种表现形式,或者编程手法,称为递归。 递归注意:1.限定条件2.注意递归次数,避免内存溢出*/import java.io.*;class FileDemo3{ public static void main(String[] args) { File f = new File("F:\\java系列教程视频\\黑马教学视频\\java练习代码"); show(f,0); } public static String getLevel(int level) { StringBuilder sb = new StringBuilder(); for(int x = 0;x<level;x++) { sb.append("|--"); } return sb.toString(); } //递归出指定文件下的所有目录 public static void show(File f,int level) { sop(getLevel(level)+f.getName()); level++; File[] files = f.listFiles(); for(int x = 0;x<files.length;x++) { if(files[x].isDirectory()) { show(files[x],level); }else{ //输出的是文件的绝对路径名 sop(getLevel(level)+files[x]); } } } public static void sop(Object obj) { System.out.println(obj); }}
3.
/*删除一个带内容的目录删除原理: 在windows中,删除目录从里面往外删除的 既然从里到外删除,就需要用到递归*/import java.io.*;class RemoveDir{ public static void main(String[] args) { File f = new File("f:\\d.txt"); removeDir(f); } //删除带内容的目录 public static void removeDir(File f) { File[] dir = f.listFiles(); for(int x = 0;x<dir.length;x++) { //如果是目录,就继续递归 if(dir[x].isDirectory()) { removeDir(dir[x]); }else { sop(dir[x]+"---file---"+dir[x].delete()); } } //删除完文件,就把此目录删除掉 sop(f+"---dir---"+f.delete()); } public static void sop(Object obj) { System.out.println(obj); }}
4.
/* 练习:将一个指定目录下的java文件的绝对路径,存储到一个文本文件中建立一个java文件列表文件思路:1.对指定的目录进行递归2.获取递归过程所有的java文件的路径3.将这些路径存储到集合中4.将集合中的数据写到一个文件中。 */import java.io.*;import java.util.*;class JavaFileList{ public static void main(String[] args) { File f = new File("F:\\java系列教程视频\\黑马教学视频\\java练习代码"); List<File> list = new ArrayList<File>(); fileToList(f,list); sop(list.size()); File javaFileList = new File(f,"javaFileList.txt"); writeToFile(list,javaFileList.toString()); } /* 将.java文件的绝对路径存储到集合中 利用递归 */ public static void fileToList(File dir,List<File> list) { File[] files = dir.listFiles(); for(File f:files) { if(f.isDirectory()) { fileToList(f,list); } else { if(f.toString().endsWith(".java")) { list.add(f); } } } } //将集合中的数据写到指定文件中 public static void writeToFile(List<File> list,String fileName) { try { BufferedWriter bw = new BufferedWriter(new FileWriter(fileName)); for(File f:list) { bw.write(f.getAbsolutePath()); bw.newLine(); bw.flush(); } }catch(IOException e) { throw new RuntimeException("error"); }finally { bw.close(); } } public static void sop(Object obj) { System.out.println(obj); }}
5.Properties的用法
/* Properties是hashtable的子类 也就是说它具备map集合的特点。而且它里面存储的键值对都是字符串。 是集合中和IO技术相结合的集合容器。 该对象的特点,可以用于键值对形式的配置文件 加载数据时,需要数据有固定格式: 键=值*/import java.util.*;import java.io.*;class PropertiesDemo{ public static void main(String[] args) throws IOException { //method_01(); loadDemo(); } public static void loadDemo() throws IOException { //从输入流中读取属性列表 FileInputStream fis = new FileInputStream("F:\\java系列教程视频\\黑马教学视频\\java练习代码\\info.txt"); Properties prop = new Properties(); prop.load(fis); sop(prop); prop.setProperty("wangwu","99"); //将属性列表写入输出流 FileOutputStream fos = new FileOutputStream("F:\\java系列教程视频\\黑马教学视频\\java练习代码\\info.txt"); prop.store(fos,"shuchu"); fis.close(); fos.close(); } //将info.txt文件中的键值存储到集合中 public static void method_01() throws IOException { BufferedReader br = new BufferedReader(new FileReader("F:\\java系列教程视频\\黑马教学视频\\java练习代码\\info.txt")); String temp = null; Properties pro = new Properties(); while((temp=br.readLine())!=null) { String[] arr = temp.split("="); pro.setProperty(arr[0],arr[1]); } sop(pro); br.close(); } //Properties的Set和Get方法 public static void setAndGet() { Properties prop = new Properties(); prop.setProperty("zhangsan","20"); prop.setProperty("lisi","22"); sop(prop); String value = prop.getProperty("lisi"); sop("value:"+value); Set<String> name = prop.stringPropertyNames(); for(String s:name) { sop("key:"+s+" value:"+prop.getProperty(s)); } } public static void sop(Object obj) { System.out.println(obj); } }
6.用于记录应用程序运行次数。如果使用次数已到,那么给出注册提示。
/*用于记录应用程序运行次数。如果使用次数已到,那么给出注册提示。很容易想到的是计数器。但是随着程序的结束,计数值在内存中也消失。所以要建立一个配置文件,用于记录该软件的使用次数。使用键值对的形式,这样便于操作数据。使用数据。键值数据是map集合,数据是以文件形式存储,使用IO技术map加IO就是properties配置文件可以实现应用程序数据的共享。*/import java.io.*;import java.util.*;class RunCount{ public static void main(String[] args) throws IOException { //配置文件info.ini File f = new File("info.ini"); //如果文件不存在就创建文件 if(!f.exists()) f.createNewFile(); //读取配置文件 FileInputStream fis = new FileInputStream(f); Properties prop = new Properties(); prop.load(fis); int count = 0; //获取文件的配置信息 String value = prop.getProperty("time"); if(value != null) count = Integer.parseInt(value); count++; //如果达到使用上限则返回 if(count == 5){ System.out.println("使用次数已到"); return; } //更改配置信息,并写入配置文件中 prop.setProperty("time",count+""); FileOutputStream fos = new FileOutputStream(f); prop.store(fos,""); fis.close(); fos.close(); }}
7.
/*打印流:该流提供了打印方法,可以将各种数据类型的数据都原样打印字节打印流PrintStream构造函数可以接收的类型:1.File对象 File2.字节输出流 OutputStream3.字符串路径 String字符打印流PrintWriter构造函数可以接收的类型:1.File对象 File2.字节输出流 OutputStream3.字符串路径 String4.字符输出流 Writer*/import java.io.*;class PrintStreamDemo{ public static void main(String[] args) throws IOException { //键盘读取的格式!!!1 BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); //printWriter输出,此构造方法意思,会自动将数据刷新到print.txt文件中 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(new File("print.txt"))),true); String temp = null; while((temp=bufr.readLine())!=null) { pw.println(temp); } bufr.close(); pw.close(); }}
8.序列流将多个读取流合并成一个流 SequenceInputStream
/* 序列流将多个读取流合并成一个流*/import java.util.*;import java.io.*;class SequenceDemo{ public static void main(String[] args) throws IOException { Vector<InputStream> v = new Vector<InputStream>(); v.add(new FileInputStream("1.txt")); v.add(new FileInputStream("2.txt")); v.add(new FileInputStream("3.txt")); //获取枚举,类型是InputStream Enumeration<InputStream> en = v.elements(); //将两个以上的流合并成一个流,构造函数必须为InputStream的枚举类型 SequenceInputStream sis = new SequenceInputStream(en); PrintStream ps = new PrintStream(new FileOutputStream(new File("4.txt"))); byte[] buff = new byte[1024]; //获取数组的长度 int len = 0; while((len=sis.read(buff))!=-1) { ps.write(buff,0,len) ; ps.println(); ps.flush(); } sis.close(); ps.close(); }}
9.
/*文件的切割,与合并合并时,利用SequenceInputStream,但是不适用Vector效率低,适用ArrayList*/import java.io.*;import java.util.*;class SplitFile{ public static void main(String[] args) throws IOException { //splitFile(); merge(); } //切割文件 public static void splitFile()throws IOException { BufferedInputStream bufr = new BufferedInputStream(new FileInputStream(new File("splitFile\\a.jpg"))); byte[] b = new byte[1024*1024]; int len = 0; int count = 1; while((len=bufr.read(b))!=-1) { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("splitFile\\"+(count++)+".part"))); bos.write(b,0,len); bos.close(); } bufr.close(); } //合并文件,利用ArrrayList获得枚举数据 public static void merge() throws IOException { ArrayList<FileInputStream> arr = new ArrayList<FileInputStream>(); arr.add(new FileInputStream(new File("splitFile\\1.part"))); arr.add(new FileInputStream(new File("splitFile\\2.part"))); final Iterator<FileInputStream> it = arr.iterator(); Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){ public boolean hasMoreElements() { return it.hasNext(); } public FileInputStream nextElement() { return it.next(); } }; SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("splitFile\\6.jpg"); byte[] b = new byte[1024]; int len = 0; while((len=sis.read(b))!=-1) { fos.write(b,0,len); } sis.close(); fos.close(); }}
10.静态不能序列化。
修饰符:transient,修饰变量后,此变量也不能被序列化。只能存在堆内存中
对象的序列化。
import java.io.*;class ObjectStreamDemo{ public static void main(String[] args) throws Exception { //writeObj(); readObj(); } public static void readObj()throws Exception { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt")); Person p = (Person)ois.readObject(); System.out.println(p); ois.close(); } public static void writeObj()throws IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt")); oos.writeObject(new Person("lisi0",399,"kr")); oos.close(); }}
11.RandomAccessFile
该类不算是IO体系中子类、
而是直接继承Object.
但是他是IO包中成员,因为它具备读和写的功能。
内部封装了一个数组,而且通过指针对数据的元素进行操作。
可以通过getFilePointer获取指针位置。
同时,可以通过seek改变指针的位置。
其实完成读写的原理就是内部封装了字节流输入和输出流。
通过构造函数可以看出,该类只能操作文件,而且操作文件还有模式。
只读r 读写rw等。
如果模式为只读r,不会创建文件,会去读取一个已存在文件,果然该文件不存在,会出现异常。
如果模式为rw,操作的文件不存在则创建,存在也不会被覆盖.
import java.io.*;class RandomAccessFileDemo{ public static void main(String[] args) throws IOException { //writeFile_2(); //readFile(); //System.out.println(Integer.toBinaryString(258)); } public static void readFile()throws IOException { RandomAccessFile raf = new RandomAccessFile("ran.txt","r"); //调整对象中指针。 //raf.seek(8*1); //跳过指定的字节数 raf.skipBytes(8); byte[] buf = new byte[4]; raf.read(buf); String name = new String(buf); int age = raf.readInt(); System.out.println("name="+name); System.out.println("age="+age); raf.close(); } public static void writeFile_2()throws IOException { RandomAccessFile raf = new RandomAccessFile("ran.txt","rw"); raf.seek(8*0); raf.write("周期".getBytes()); raf.writeInt(103); raf.close(); } public static void writeFile()throws IOException { RandomAccessFile raf = new RandomAccessFile("ran.txt","rw"); raf.write("李四".getBytes()); raf.writeInt(97); raf.write("王五".getBytes()); raf.writeInt(99); raf.close(); }}
12.DataInputStream 可以用于操作基本数据类型
/*DataInputStream与DataOutputStream可以用于操作基本数据类型的数据的流对象。*/import java.io.*;class DataStreamDemo{ public static void main(String[] args) throws IOException { //writeData(); //readData(); //writeUTFDemo();// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"),"gbk");//// osw.write("你好");// osw.close();// readUTFDemo(); } public static void readUTFDemo()throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream("utf.txt")); String s = dis.readUTF(); System.out.println(s); dis.close(); } public static void writeUTFDemo()throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("utfdate.txt")); dos.writeUTF("你好"); dos.close(); } public static void readData()throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream("data.txt")); int num = dis.readInt(); boolean b = dis.readBoolean(); double d = dis.readDouble(); System.out.println("num="+num); System.out.println("b="+b); System.out.println("d="+d); dis.close(); } public static void writeData()throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt")); dos.writeInt(234); dos.writeBoolean(true); dos.writeDouble(9887.543); dos.close(); }}
13.管道类PipedInputStream
import java.io.*;class Read implements Runnable{ private PipedInputStream in; Read(PipedInputStream in) { this.in = in; } public void run() { try { byte[] buf = new byte[1024]; System.out.println("读取前。。没有数据阻塞"); int len = in.read(buf); System.out.println("读到数据。。阻塞结束"); String s= new String(buf,0,len); System.out.println(s); in.close(); } catch (IOException e) { throw new RuntimeException("管道读取流失败"); } }}class Write implements Runnable{ private PipedOutputStream out; Write(PipedOutputStream out) { this.out = out; } public void run() { try { System.out.println("开始写入数据,等待6秒后。"); Thread.sleep(6000); out.write("piped lai la".getBytes()); out.close(); } catch (Exception e) { throw new RuntimeException("管道输出流失败"); } }}class PipedStreamDemo{ public static void main(String[] args) throws IOException { PipedInputStream in = new PipedInputStream(); PipedOutputStream out = new PipedOutputStream(); in.connect(out); Read r = new Read(in); Write w = new Write(out); new Thread(r).start(); new Thread(w).start(); }}
0 0
- 黑马程序员---2015.6.26java基础笔记--File类--Properties--PrintStream--SequenceInputStream--RandomAccessFile
- 黑马程序员_学习笔记:14) Properties、PrintStream、SequenceInputStream:序列流、RandomAccessFile、管
- 黑马程序员 java 基础 毕向东 面向对象 IO操作 File Properties PrintWriter PrintStream 应用
- 黑马程序员—SequenceInputStream和Properties类
- Java基础 IO流 File Properties PrintWriter 序列流SequenceInputStream
- 黑马程序员--基础--第二篇--SequenceInputStream和Properties类特殊类
- 黑马程序员------Properties和RandomAccessFile类
- 黑马程序员-常用的IO类使用,PrintStream,SequenceInputStream,文件切割,ObjectStream
- 黑马程序员——Java基础——IO流笔记(SequenceInputStream使用示例)
- 黑马程序员——Java基础---File、Properties、序列流
- 黑马程序员——Java基础——IO流笔记(PrintStream使用示例)
- 黑马程序员——Java基础---IO流(File类、Properties类、其他流)
- 黑马程序员--java基础--File类、Properties类、打印流、序列流
- 黑马程序员 Java基础<十>---> IO流<2>(File类,Properties,打印流,合并流)
- 黑马程序员——Java基础——File、Properties类、 打印流、序列流
- 黑马程序员——java基础--File类、Properties、管道流、打印流、编码
- 黑马程序员——Java基础——File、Properties类
- 14.IO(三)【File】【Properties】【打印流PrintStream】【序列流SequenceInputStream】
- IOS--C 语言 Lesson Eight Struct
- Stanford机器学习---第十一讲.异常检测
- iOS UILabel
- shopnc b2b2c初次运行提示Class Error: mysqli.isn't exists!的解决方法
- zoj2412
- 黑马程序员---2015.6.26java基础笔记--File类--Properties--PrintStream--SequenceInputStream--RandomAccessFile
- WebView控件应用——模仿URL有道词典
- 为什么使用portlet?
- 金士顿KTD-INSP6000C/2G内存
- iOS-学习笔记-UI-第十九天
- 在Android APK中调用底层linux命令或者脚本
- JS Library Set
- springLdap 操作ldap示例(增删改查)
- 升级旧Delphi应用转向支持手机的一个思路