关于Java基础的文章,我觉得写得还可以,以前发在了我其它的博客了,肯定是原创,现在再分享给大家出来。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在变量数组中和对象中存放的数据是暂时的,程序结束后就会丢失。为了能够永久的保存数据,需要将其存储在磁盘中。
java中的I/O技术可以将数据保存到本地,以达到永久保存的要求。
流是一组有序的序列,根据操作的类型,可分为输入流和输出流。
字节流的两个顶层父类:
1,InputStream 2,OutputStream.
字符流的两个顶层父类:
1,Reader 2,Writer
这些体系的子类都以父类名作为后缀。
而且子类名的前缀就是该对象的功能。
如图:
其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表。获取对应的文字。
在对这个文字进行操作。简单说:字节流+编码表
a、FileWirter写入文本
<span style="font-size:14px;">FileWriter fw = new FileWriter("d:\\jinfulin.txt"); w.write("dfs"); fw.close();</span>
b、FileReader读取文本
<span style="font-size:14px;">FileReader fr = new FileReader("d:\\jinfulin.txt"); int num1 = fr.read();//读取第一个字符 int num2 = fr.read();//再次调用读取第二个字符(流特性嘛) System.out.println((char)num1);//打印,将数字转成字符 System.out.println((char)num2);</span>
<span style="font-size:14px;"> FileReader fr = new FileReader("demo.txt"); /* * 使用read(char[])读取文本文件数据。 * * 先创建字符数组。 */ char[] buf = new char[1024]; int len = 0; while((len=fr.read(buf))!=-1){//没有字符了读到数据位-1 System.out.println(new String(buf,0,len)); } <span style="white-space:pre"> </span>fr.close();</span>
<span style="font-size:14px;"> private static void myStreamCopy() throws IOException { //定义输入输出流并加缓冲区 BufferedOutputStream buffo = new BufferedOutputStream(new FileOutputStream("f:\\copy.mp3")); BufferedInputStream buffi = new BufferedInputStream(new FileInputStream("f:\\风吹麦浪.mp3")); //开始复制 int ch = 0; while((ch = buffi.read()) != -1) { buffo.write(ch); } //关闭流 buffo.close(); buffi.close(); }</span>
InputStreamReader :字节到字符的桥梁。解码。OutputStreamWriter:字符到字节的桥梁。编码。
<span style="font-size:14px;"><span style="white-space:pre"> </span>/* * 将控制台输入的文字存储到文件中。 */ public static void main(String[] args) throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("F:\\jinfulin.txt"))); String line = null; while((line = bufr.readLine()) != null) { if("over".equals(line)) break; bufw.write(line); System.out.println(line); } bufr.close(); bufw.close(); } </span>
因为流对象太多,开发时不知道用哪个对象合适。弄清规律有助于我们使用。
1,明确源和目的
源:InputStream Reader
目的:OutputStream Writer
2,明确数据是否是纯文本数据。
源:是纯文本:Reader
---------否:InputStream
目的:是纯文本 Writer
------否:OutputStream
3,明确具体的设备。
源设备:
硬盘:File
键盘:System.in
内存:数组
网络:Socket流
目的设备:
硬盘:File
控制台:System.out
内存:数组
网络:Socket流
4,是否需要其他额外功能。
1,是否需要高效(缓冲区);
2,转换。
<span style="font-size:14px;">//可以将一个已存在的,或者不存在的文件或者目录封装成file对象。 File f1 = new File("c:\\a.txt"); File f2 = new File("c:\\","a.txt"); File f = new File("c:\\"); File f3 = new File(f,"a.txt"); File f4 = new File("c:"+File.separator+"abc"+File.separator+"a.txt");</span>
<span style="font-size:14px;"><span style="white-space:pre"> </span>File file = new File("a.txt"); String name = file.getName();//获取文件名(a.txt) String absPath = file.getAbsolutePath();//绝对路径。 String path = file.getPath();//相对路径 long len = file.length();//文件长度(大小) long time = file.lastModified();//最后修改时间</span>
<span style="font-size:14px;">File dir = new File("f:\\a1\\22\\jin.txt"); boolean b = dir.mkdir();//创建文件 dir.mkdirs();//创建多级目录 System.out.println(dir.delete()); boolean b = file.createNewFile();//如果不存在就创建 </span>c、判断
<span style="font-size:14px;"> boolean b = f.exists(); // 判断是否存在。 System.out.println(f.isFile());//是否是文件 System.out.println(f.isDirectory());//是否是一个目录</span>
<span style="font-size:14px;"><span style="white-space:pre"> </span>File f1 = new File("c:\\kkk.mp3"); File f2 = new File("d:\\ccc.mp3"); boolean b = f1.renameTo(f2); System.out.println("ccc="+b);</span>
<span style="font-size:14px;"> String[] names = file.list(); <span style="white-space:pre"> </span>if(names == null)//如果为空就返回,避免出现空指针异常 <span style="white-space:pre"> </span>return; System.out.println(names.length); for(String name : names){ System.out.println(name); }</span>
<span style="font-size:14px;">public class DeleteAll { public static void main(String[] args) { File dir = new File("f:\\1234"); // dir.delete();//直接删是删不掉的 dirdelete(dir); } private static void dirdelete(File dir) { File[] files = dir.listFiles(); if (files == null) return; for (File file : files) { if(file.isDirectory()){//如果是目录就递归 dirdelete(file); } //不是目录就删掉 System.out.println(file.getAbsolutePath() + "----"+ file.delete()); } //最后再删掉根目录的文件夹(此时已经空了) System.out.println(dir.getAbsolutePath() + "----"+ dir.delete()); } }</span>
Properties类并不属于IO包中的类,而是属于map集合,在前面map集合章节也有提到,不过由于该集合中数据用到流,在这里说更合适。
1,该集合中的键和值都是字符串类型。
2,集合中的数据可以保存到流中,或者从流获取。 通常该集合用于操作以键值对形式存在的配置文件。
定义功能,获取一个应用程序运行的次数,如果超过5次,给出使用次数已到请注册的提示。并不要在运行程序。
<span style="font-size:14px;">public static void getAppCount() throws IOException{ //将配置文件封装成File对象。 File confile = new File("count.properties"); if(!confile.exists()){//健壮性判断 confile.createNewFile(); } //集合中的数据来自于一个文件。 //注意;必须要保证该文件中的数据是键值对。 //需要使用到读取流。 FileInputStream fis = new FileInputStream(confile); Properties prop = new Properties(); prop.load(fis);//集合中加载一个文件输入流 //从集合中通过键获取次数。 String value = prop.getProperty("time"); //定义计数器。记录获取到的次数。 int count =0; if(value!=null){ count = Integer.parseInt(value); if(count>=5){ throw new RuntimeException("使用次数已到,请注册,给钱!"); } } count++; //将改变后的次数重新存储到集合中。 //将集合中数据存储到文件中,使用store方法。 prop.setProperty("time", count+""); FileOutputStream fos = new FileOutputStream(confile); prop.store(fos, "注释....."); fos.close(); fis.close(); }</span>
由于I/O这部分知识比较多,也比较重要,所有我分成两篇博客来写,欢迎大家看下一篇博客。
原文地址:http://blog.csdn.net/jinfulin/article/details/44946703