通过几个小实例来抛析文件IO的读写

来源:互联网 发布:linux网络连接命令 编辑:程序博客网 时间:2024/06/06 15:49

第一个:小实例对文件的基本操作和经常遇到的相关APi的简介

public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub//创建文件对象//存放文件的基本信息File file = new File("E:/a.txt");//测试文件是否存在System.out.println(file.exists());if(!file.exists()){try {file.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//得到文件大小,以字节为单位System.out.println(file.length());//得到文件最近一次的修改日期System.out.println(file.lastModified());//简单的日期格式化SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//创建日期对象Date date = new Date(file.lastModified());//执行格式化操作System.out.println(sdf.format(date));File director = new File("E:/my");if(!director.exists()){director.mkdir(); //创建文件夹}//File对象既可以表示文件也可以表示文件夹//得到my文件夹下所有文件对象。File files[] = director.listFiles();for (int i = 0; i < files.length; i++) {System.out.println(files[i].getName()); //得到文件对象的文件名}}}
第二个小实例:对io流的操作,字符流和字节流的读写


public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFile file = new File("E:/a.txt");//使用字符流读取文本文件中的内容try {FileReader fileReader = new FileReader(file);char c[] = new char[2];StringBuffer sb = new StringBuffer();int len = 0;//将读到的内容存放在字符数组c中//len存放读到的字符个数,当读到最后时,得到-1。while((len = fileReader.read(c)) != -1){String s = new String(c, 0, len);sb.append(s);System.out.println("len = " + len);System.out.println("s = " + s);}System.out.println(sb.toString());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//字符流写数据File fileB = new File("E:/b.txt");if(!fileB.exists()){try {fileB.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}try {FileWriter fileWrite = new FileWriter(fileB);String s = "abcccccc\r\nabc";//写到缓冲区fileWrite.write(s);//刷新fileWrite.flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//字节流读数据byte b[] = null;File filePic = new File("E:/emoticon_01.png");try {FileInputStream fis = new FileInputStream(filePic);//fis.available() 有效字节数System.out.println(fis.available());b = new byte[fis.available()];fis.read(b);System.out.println(fis.available());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//字节流写数据File fileCopy = new File("E:/my/emoticon_01.png");if(!fileCopy.exists()){try {fileCopy.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}try {FileOutputStream fps = new FileOutputStream(fileCopy);fps.write(b);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

第三个小实例:缓冲读写
public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub//BufferedReader 缓冲读(一行一行的读)File file = new File("E:/a.txt");try {FileReader fr = new FileReader(file);BufferedReader br = new BufferedReader(fr);String line = null;StringBuffer sb = new StringBuffer();while((line = br.readLine()) != null){sb.append(line+"\n");}System.out.println(sb.toString());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//BufferedWriter 缓冲写(一行一行的写)File fileW = new File("E:/c.txt");try {FileWriter fw = new FileWriter(fileW);BufferedWriter bw = new BufferedWriter(fw);bw.write("line\r\n");bw.write("one");//关闭流 (最先打开的流,最后关闭)bw.close();fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

第四个小实例:字节流和字符流的转换

public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub//字节流和字符流的转换//使用字节流读文本数据try {FileInputStream fis = new FileInputStream(new File("E:/a.txt"));InputStreamReader isr = new InputStreamReader(fis, "utf-8");char c[] = new char[1024];int len = 0;while((len = isr.read(c)) != -1){String s = new String(c, 0, len);System.out.println(s);}//byte b[] = new byte[fis.available()];//fis.read(b);//////将字节数组转化为字符串//String s = new String(b);//System.out.println(s);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


第五个小实例:对实现序列化接口的类进行读写,下面有两个类,一个book类,一个测试类,以及注释供大家参考

book.java

//当前类实现序列化接口。//序列化接口用来压缩对象public class Book implements Serializable {private String name;public Book(String name) {// TODO Auto-generated constructor stubthis.name = name;}public String getName() {return name;}}

下面是测试类

public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFile file = new File("E:/object.txt");if(!file.exists()){try {file.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}try {FileOutputStream fos = new FileOutputStream(file);//写对象字节流ObjectOutputStream oss = new ObjectOutputStream(fos);Book book = new Book("think in java");oss.writeObject(book);oss.close();fos.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {FileInputStream fis = new FileInputStream(file);//读对象字节流ObjectInputStream ois = new ObjectInputStream(fis);Book book = (Book)ois.readObject();System.out.println(book.getName());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


0 0
原创粉丝点击