Java学习笔记 —— 22文件
2014-11-06 15:39
387 查看
文件
文件流
Java IO流类
分类
File实例
FileInputStream实例
FileOutputStream实例
ImageStream
文件字符流
Reader
Writer
缓冲字符流
BufferedReader
BufferedWriter
文件流
Java IO流类
分类
File实例
import java.io.File; import java.io.IOException; public class Demo2 { public static void main(String[] args) { // TODO Auto-generated method stub //打开一个文件 File f = new File("d:\\aa.txt"); System.out.println("文件路径" + f.getAbsolutePath()); System.out.println("剩余空间" + f.getFreeSpace()); //创建文件 File f2 = new File("d:\\fasd.txt"); if ( !f2.exists() ) { //创建文件 try { f.createNewFile(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } } else { System.out.println("文件已经存在"); } //创建文件和文件夹 File f3 = new File("d:\\ff"); if( !f3.isDirectory() ){ try { f3.mkdir(); } catch (Exception e) { // TODO: handle exception } } else { System.out.println("文件夹已经存在"); } //列出一个文件夹下面的所有文件 File f4 = new File("D:\\ff"); if ( f4.isDirectory()) { File[] lists = f4.listFiles(); for (int i = 0; i < lists.length; i++) { System.out.println("list" + i + ":" + lists[i].getName()); } } } }
FileInputStream实例
import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class Demo3 { public static void main(String[] args) { File f = new File("D:/aa.txt"); FileInputStream fis = null; //引文File没有读写能力,所以需要使用InputStream try { fis = new FileInputStream(f); //定义一个字节数组 byte[] bytes = new byte[2048]; int n = 0; //循环读取 while( (n=fis.read(bytes)) != -1){ //吧字节转换成String String s = new String(bytes, 0 , n); System.out.println(s); } } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } finally { //关闭文件流必须放这里 try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
FileOutputStream实例
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class TestFileOutputStream { public static void main(String[] args) { // TODO Auto-generated method stub File f = new File("d:/aa.txt"); FileOutputStream fos = null; try { fos = new FileOutputStream(f); String s = "把地球甩掉\r\n"; String s2 = "一颗心扑通扑通扑通地乱跳"; //定义字节数组 fos.write(s.getBytes()); fos.write(s2.getBytes()); } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } finally { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
ImageStream
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class TestImage { public static void main(String[] args) { //先把图片读入到内存 ——》写入某个文件 //因为是二进制文件,因此使用字节流完成。 //输入流 FileInputStream fis = null; //输出流 FileOutputStream fos = null; try { fis = new FileInputStream("D:/3.PNG"); fos = new FileOutputStream("D:/computer.PNG"); byte[] buf = new byte[512]; int n = 0; //循环读取 while ((n = fis.read(buf)) != -1) { //输出到指定文件。 fos.write(buf); } } catch (Exception e) { // TODO: handle exception } finally { try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
文件字符流
Reader
Writer
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class TestReaderWriter { public static void main(String[] args) { //文件中取出字符流对象(输入流) FileReader fr = null; //写入文件(输出流) FileWriter fw = null; try { //创建dr对象 fr = new FileReader("D:/test.txt"); int n = 0; //读入到内存 char c[] = new char[1024]; while ( (n = fr.read(c))!= -1) { String s = new String(c,0,n); System.out.println(s); } while ( (n = fr.read(c)) != -1) { fw.write(c, 0 ,n); } } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } finally { try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
缓冲字符流
BufferedReader
BufferedWriter
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class TestBufferRW { public static void main(String[] args) { BufferedReader br = null; BufferedWriter bw = null; try { //先创建FileReader对象 FileReader fr = new FileReader("D:/test.txt"); br = new BufferedReader(fr); //先创建FileWriter对象 FileWriter fw = new FileWriter("D:/my.txt"); bw = new BufferedWriter(fw); //循环读取文件 String s = ""; while ( (s = br.readLine()) != null) { System.out.println(s); } //循环写文件 String s2 = ""; while ( (s2 = br.readLine()) != null) { bw.write(s2 + "\r\n"); } } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } finally { try { br.close(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
相关文章推荐
- Java 实现 文件目录 遍历 - Java 学习笔记 (22)
- [学习笔记]Java 中对文件的读写操作之比较
- java_croe 学习笔记之新IO---java.nio 之内存映射文件
- Java学习笔记(2)----Web项目中解析XML文件
- Java 学习笔记22:Maven以及其Eclipse插件m2eclipse的安装
- java学习笔记_将中文字符导出txt文档_从txt文档读取文件
- Java学习笔记 10——文件和流
- Java与Flex学习笔记(8)----Flex带进度条的多文件上传(基于Servlet)
- java之jvm学习笔记九(策略文件)
- java学习笔记---第12章流于文件
- java读文件学习笔记
- java反射学习笔记三(改进笔记二放到配置文件中)
- Java 学习笔记04:Spring XML配置文件Bean
- JAVA学习笔记(五)-文件和安全性
- JAVA文件-基本操作学习笔记
- Java基础学习笔记(十一)I/O 文件访问
- java虚拟机学习笔记——java class文件的内容(第六章)
- 【Java学习笔记】操作JAR文件
- java学习笔记6 - 使用commons configuration管理配置文件
- IOS学习笔记22—文件操作(NSFileManager)结合相册小例子