FileOutputStream

 1 package cn.hello;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.FilenameFilter;
 7 import java.io.IOException;
 8 import java.text.SimpleDateFormat;
 9 import java.util.Date;
10 
11 /*
12  * IO流
13  * 作用:主要是用来上传文件和下载文件
14  * 字节流
15  * 字符流:如果是文本,用字符流
16  * 
17  * 字节流
18  *         InputStream  OutputStream
19  * 字符流
20  *         Reader    Writer
21  * 
22  * 
23  * */
24 
25 /*
26  * OutputStream
27  *     构造方法
28  *             FileOutputStream(File file)
29  *             FileOutputStream(String name)
30  * 
31  * */
32 public class Test01 {
33     public static void main(String[] args) throws IOException {
34         //创建字节输出流对象
35         /*File f1=new File("j:\\zf");
36         f1.mkdir();
37         File f2=new File(f1,"a.txt");
38         try {
39             f2.createNewFile();
40         } catch (IOException e) {
41             // TODO Auto-generated catch block
42             e.printStackTrace();
43         }
44         
45         try {
46             FileOutputStream fos=new FileOutputStream(f2);
47         } catch (FileNotFoundException e) {
48             // TODO Auto-generated catch block
49             e.printStackTrace();
50         }
51         */
52         
53         
54         //第二种方式
55         //1创建字节流对象
56         FileOutputStream fos=new FileOutputStream("j:\\hello.txt");
57         //创建字节流对象做了:1调用系统功能去创建文件(但是不会创建目录)   2创建fos对象   3把fos对象指向这个文件
58         
59         //2调用方法 写入数据
60         //把字符串转为字节数组:getBytes()    转为字符数组:toCharArray()
61         fos.write("hello world".getBytes());
62         
63         //3释放资源
64         fos.close();   //1让流对象变成垃圾,被垃圾回收器回收2通知系统去释放资源
65         
66     }
67 }

 

 1 package cn.hello;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.FilenameFilter;
 7 import java.io.IOException;
 8 import java.text.SimpleDateFormat;
 9 import java.util.Date;
10 
11 /*实现换行
12  * 
13  *     linux:    \n
14  *     mac:     \r
15  *     windows    \r\n
16  * */
17 public class Test01 {
18     public static void main(String[] args) throws IOException {
19         File f1=new File("j:\\zf\\zf");
20         f1.mkdirs();
21         FileOutputStream fos=new FileOutputStream("j:\\zf\\zf\\hello.txt");
22         fos.write("hello world".getBytes());
23         
24         //换行
25         for(int x=0;x<10;x++){
26             fos.write(("hello"+x).getBytes());
27             fos.write("\r\n".getBytes());
28         }
29         
30         //追加写入
31         FileOutputStream fos1=new FileOutputStream("j:\\zf\\zf\\hello.txt",true);
32         fos1.write("hello world take it easy".getBytes());
33         fos.close();
34         
35     }
36 }
 1 package cn.hello;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.FilenameFilter;
 8 import java.io.IOException;
 9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 
12 
13 /*
14  * 读出数据
15  * FileInputStream
16  *         
17  *                 
18  * 
19  * 
20  * */
21 
22 public class Test01 {
23     public static void main(String[] args) throws IOException {
24         FileInputStream fis=new FileInputStream("j:\\zf\\zf\\hello.txt");
25         //第一次读取
26     /*    int by=fis.read();
27         System.out.print((char)by);
28         //第二次读取
29         by=fis.read();
30         System.out.print((char)by);
31         //第三次读取
32         by=fis.read();
33         System.out.print((char)by);*/
34         /*
35          * 由上面观察可知,文件读取存在类似于指针的东西,会在上一次读取位置基础上,往后继续读取
36          * */
37         
38     /*    int by1=fis.read();
39         while(by1!=-1){
40             System.out.print((char)by1);
41             by1=fis.read();
42         }*/
43         
44         int by=0;
45         while((by=fis.read())!=-1){
46             System.out.print((char)by);                         //文件里如果有中文,则会出现乱码,以后会用字符流处理
47         }
48         
49         fis.close();
50     }
51 }

 

 1 package cn.hello;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.FilenameFilter;
 8 import java.io.IOException;
 9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 
12 
13 /*
14  * 复制文件
15  * 
16  * */
17 public class Test01 {
18     public static void main(String[] args) throws IOException {
19         //封装数据源
20         FileInputStream fis=new FileInputStream("j:\\zf\\zf\\hello.txt");
21         //封装目的地
22         FileOutputStream fos=new FileOutputStream("j:\\zf\\zf\\world.txt");
23         
24         int by=0;
25         while((by=fis.read())!=-1){                                     //此时传输中文是没有问题的,因为中间没有转换过程
26             fos.write(by);
27         }
28         
29         fis.close();
30         fos.close();
31     }
32 }
 1 package cn.hello;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.FilenameFilter;
 8 import java.io.IOException;
 9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 
12 /*
13  * 一次读取一个字节数组
14  * int read(byte[] b)
15  * */
16 
17 public class Test01 {
18     public static void main(String[] args) throws IOException {
19         //封装数据源
20         FileInputStream fis=new FileInputStream("j:\\zf\\zf\\101.avi");
21         //封装目的地
22         FileOutputStream fos=new FileOutputStream("j:\\zf\\222.avi");
23         
24         byte[] bys=new  byte[1024];
25         int len=fis.read(bys);
26         
27         while((len=fis.read(bys))!=-1){
28             fos.write(bys,0,len);
29         }
30         
31         fis.close();
32         fos.close();
33     }
34 }

 

 1 package cn.hello;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.FilenameFilter;
 9 import java.io.IOException;
10 import java.text.SimpleDateFormat;
11 import java.util.Date;
12 
13 
14 /*
15  * 字节缓冲流
16  * BufferedOutputStream          BufferedInputStream
17  * 
18  * 构造方法可以指定缓冲区的大小,但一般用不上,因为默认缓冲区大小就足够了
19  * 
20  * 
21  * */
22 
23 public class Test01 {
24     public static void main(String[] args) throws IOException {
25         //BufferedOutputStream(OutputStream out)     ?为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?
26         //因为字节缓冲区仅仅提供缓冲区,是为搞笑设计的。但是真正的读写操作还是靠基本的流对象实现
27         //FileOutputStream fos=new FileOutputStream("j:\\zf\\zf\\hello.txt");
28         BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("j:\\zf\\zf\\hello.txt"));
29         
30         bos.write("hello".getBytes());
31         
32         bos.close();
33     }
34 }

 

 1 package cn.hello;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.FilenameFilter;
 9 import java.io.IOException;
10 import java.io.OutputStreamWriter;
11 import java.text.SimpleDateFormat;
12 import java.util.Arrays;
13 import java.util.Date;
14 
15 /*
16  *         String(byte[] bytes,String charsetName):通过制定的字符集解码字节数组
17  *         
18  *         byte[] getBytes(String charsetName):使用指定的字符集和把字符串编码为字节数组
19  * 
20  * 编码:把看的懂得转为看不懂的
21  *         OutputStreamWriter(outputStream out)  使用默认编码
22  *         OutputStreamWriter(outputStream out,String charsetName)  使用指定编码
23  * 
24  * */
25 
26 public class Test01 {
27     public static void main(String[] args) throws IOException {
28         /*    String s="你好";
29             byte[] bys=s.getBytes("GBK");
30             System.out.println(Arrays.toString(bys));*/
31         //字符流=字节流+编码表
32         
33         //创建对象
34         OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("j:\\zf\\zf\\hello.txt"),"GBK");
35         
36         osw.write("中国");
37         
38         osw.close();
39     }
40 }
 1 package cn.hello;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.FilenameFilter;
 9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.OutputStreamWriter;
12 import java.text.SimpleDateFormat;
13 import java.util.Arrays;
14 import java.util.Date;
15 
16 /*
17  * 
18  * InputStreamReader
19  * */
20 
21 public class Test01 {
22     public static void main(String[] args) throws IOException {
23         InputStreamReader isr=new InputStreamReader(new FileInputStream("j:\\zf\\zf\\hello.txt"),"UTF-8");
24         
25         int ch=0;
26         
27         while((ch=isr.read())!=-1){
28             System.out.print((char)ch);
29         }
30     }
31 }
 1 package cn.hello;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.FilenameFilter;
 9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.OutputStreamWriter;
12 import java.text.SimpleDateFormat;
13 import java.util.Arrays;
14 import java.util.Date;
15 
16 /*
17  * OutputStreamWriter
18  *     public void write(int c):写一个字符
19  * public void write(char[] cbuf):写一个字符数组
20  * public void write(char[] cbuf ,int off,int len)
21  * public void write(String str):写一个字符串
22  * public void write(String str,int off,int len)
23  * 
24  * */
25 
26 public class Test01 {
27     public static void main(String[] args) throws IOException {
28             //创建对象
29             OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("j:\\zf\\zf\\hello.txt"));
30             osw.write('林');     //为什么文件还是空的呢?  因为:字符=2字节     文件中数据存储的基本单位是字节        有时候需要flush一下
31             //osw.flush();
32             
33             //public void write (char[] cbuf)
34             char[] chs={'a','b','c','d'};
35             osw.write(chs);
36             
37             osw.write("敢与士大夫日月争辉",2,3);
38             //刷新缓冲区
39             //osw.flush();
40             
41             osw.close();   //关闭之前会自动flush一下
42     }
43 }
 1 package cn.hello;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.FilenameFilter;
 9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.OutputStreamWriter;
12 import java.text.SimpleDateFormat;
13 import java.util.Arrays;
14 import java.util.Date;
15 
16 /*
17  * InputStreamReader
18  *         int read()
19  *         int read(char[] chs)
20  * 
21  * */
22 
23 public class Test01 {
24     public static void main(String[] args) throws IOException {
25         //创建对象
26         InputStreamReader isr=new InputStreamReader(new FileInputStream("j:\\zf\\zf\\hello.txt"));
27         //一次读取一个字符
28         /*int ch=0;
29         while((ch=isr.read())!=-1){
30             System.out.print((char)ch);
31         }*/
32         
33         //一次读取一个字符
34         char[] chs=new char[1024];
35         int len=0;
36         while((len=isr.read(chs))!=-1){
37             System.out.print(new String(chs,0,len));
38         }
39         
40         
41         
42         isr.close();
43     }
44 }
 1 package cn.hello;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.FilenameFilter;
 9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.OutputStreamWriter;
12 import java.text.SimpleDateFormat;
13 import java.util.Arrays;
14 import java.util.Date;
15 
16 /*
17  * 字符流:读写文件
18  * */
19 
20 public class Test01 {
21     public static void main(String[] args) throws IOException {
22         //封装数据源
23         InputStreamReader isr=new InputStreamReader(new FileInputStream("j:\\zf\\zf\\hello.txt"));
24         //封装目的地
25         OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("j:\\zf\\zf\\hhh.txt"));
26         
27         //方式1
28     /*    int ch=0;
29         while((ch=isr.read())!=-1){
30             osw.write(ch);
31         }*/
32         
33         //方式2
34         char[] chs=new char[1024];
35         int len=0;
36         while((len=isr.read(chs))!=-1){
37             osw.write(chs,0,len);
38             osw.flush();
39         }
40         
41         osw.close();
42         isr.close();
43     }
44 }
 1 package cn.hello;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.FileReader;
 9 import java.io.FileWriter;
10 import java.io.FilenameFilter;
11 import java.io.IOException;
12 import java.io.InputStreamReader;
13 import java.io.OutputStreamWriter;
14 import java.text.SimpleDateFormat;
15 import java.util.Arrays;
16 import java.util.Date;
17 
18 /*
19  * 
20  * 由于我们常见的操作都是使用本地默认编码,所以,不用指定编码,而转换流有点长,所以,java就提供了其子类供我们使用
21  * 
22  * OutputStreamWriter=FileOutputStream+编码表(GBK)
23  * FileWriter=FileOutputStream+编码表(GBK)
24  * 
25  * 字符流的简化写法
26  * FileReader   FileWriter
27  * */
28 
29 public class Test01 {
30     public static void main(String[] args) throws IOException {
31             //封装数据源
32             FileReader fr=new FileReader("j:\\zf\\zf\\hello.txt");
33             //封装目的地
34             FileWriter fw=new FileWriter("j:\\zf\\zf\\sss.txt");
35             
36             //一次一个字符
37             int ch=0;
38             while((ch=fr.read())!=-1){
39                 fw.write(ch);
40             }
41             
42             //一次一个字符数组
43             char[] chs=new char[1024];
44             int len=0;
45             while((len=fr.read(chs))!=-1){
46                 fw.write(chs,0,len);
47                 fw.flush();
48             }
49             
50             fw.close();
51             fr.close();
52             
53             
54     }
55 }
 1 package cn.hello;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.BufferedReader;
 5 import java.io.BufferedWriter;
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.FileNotFoundException;
 9 import java.io.FileOutputStream;
10 import java.io.FileReader;
11 import java.io.FileWriter;
12 import java.io.FilenameFilter;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15 import java.io.OutputStreamWriter;
16 import java.text.SimpleDateFormat;
17 import java.util.Arrays;
18 import java.util.Date;
19 
20 /*
21  * 字符缓冲流
22  * 字符缓冲流的特殊方法
23  * BufferedWriter:
24  * public void newLine():根据系统来决定换行
25  * BufferedReader:
26  * public String readLine():一次读取一行数据                      返回的只是改行的内容,不包括换行符   到达流的末尾的时候,返回的不是-1而是null
27  * 
28  * */
29 
30 public class Test01 {
31     public static void main(String[] args) throws IOException {
32         //BufferedWriter(Writer out)
33         BufferedWriter bw=new BufferedWriter(new FileWriter("j:\\zf\\zf\\yyy.txt"));
34         
35         bw.write("hello world take it easy ");
36         
37         //字符缓冲流的特殊方法
38         bw.newLine();
39         bw.write("hello,can we  go to another place so we can go there and we will go to ");
40         bw.flush();
41         
42         bw.close();
43             
44         //-------------------------------    一次读出一行数据
45         BufferedReader br=new BufferedReader(new FileReader("j:\\zf\\zf\\yyy.txt"));
46         System.out.println(br.readLine());
47         //---------
48         String line=null;
49         while((line=br.readLine())!=null){
50             System.out.println(line);
51         }
52         br.close();
53         
54     }
55 }
 1 package cn.hello;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.BufferedReader;
 5 import java.io.BufferedWriter;
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.FileNotFoundException;
 9 import java.io.FileOutputStream;
10 import java.io.FileReader;
11 import java.io.FileWriter;
12 import java.io.FilenameFilter;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15 import java.io.OutputStreamWriter;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Date;
20 
21 /*
22  * 把集合中的数据存储到文本文件中
23  * */
24 
25 public class Test01 {
26     public static void main(String[] args) throws IOException {
27         ArrayList<String>  arr=new ArrayList<String>();
28         arr.add("hello");
29         arr.add("world");
30         arr.add("java");
31         
32         BufferedWriter bw=new BufferedWriter(new FileWriter("j:\\zf\\zf\\hello.txt"));
33         
34         for(String s:arr){
35             bw.write(s);
36             bw.newLine();
37             bw.flush();
38         }
39         bw.close();
40     }
41 }
 1 package cn.hello;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.BufferedReader;
 5 import java.io.BufferedWriter;
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.FileNotFoundException;
 9 import java.io.FileOutputStream;
10 import java.io.FileReader;
11 import java.io.FileWriter;
12 import java.io.FilenameFilter;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15 import java.io.OutputStreamWriter;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Date;
20 
21 /*
22  * 把文本文件中的数据存储到集合中
23  * */
24 
25 public class Test01 {
26     public static void main(String[] args) throws IOException {
27         //封装数据源
28         BufferedReader br=new BufferedReader(new FileReader("j:\\zf\\zf\\ct.txt"));
29         //封装目的地
30         ArrayList<String> arr=new ArrayList<String>();
31         
32         //读取数据,存储到集合中
33         String line=null;
34         while((line=br.readLine())!=null){
35             arr.add(line);
36         }
37         br.close();
38         //遍历集合
39         for(String s:arr){
40             System.out.println(s);
41         }
42         
43         
44     }
45 }
 1 package cn.hello;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.BufferedReader;
 5 import java.io.BufferedWriter;
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.FileNotFoundException;
 9 import java.io.FileOutputStream;
10 import java.io.FileReader;
11 import java.io.FileWriter;
12 import java.io.FilenameFilter;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15 import java.io.OutputStreamWriter;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Date;
20 import java.util.Random;
21 
22 /*
23  * 一个文本文件中存储了几个名字,实现如何随机获取一个人的名字
24  * 分析:
25  *             1:把文本文件中的内容放入集合中
26  *             2:随机产生一个索引
27  *             3:根据该索引获取一个值
28  * 
29  * 
30  * */
31 
32 public class Test01 {
33     public static void main(String[] args) throws IOException {
34         //1把文本文件中的内容放入集合中
35         BufferedReader br=new BufferedReader(new FileReader("j:\\zf\\zf\\ct.txt"));
36         
37         ArrayList<String> arr=new ArrayList<String>();
38         String line=null;
39         while((line=br.readLine())!=null){
40             arr.add(line);
41         }
42         br.close();
43         
44         //随机产生一个索引
45         Random r=new Random();
46         int index=r.nextInt(arr.size());
47         
48         //根据索引获取一个值
49         String name=arr.get(index);
50         System.out.println(name);
51     }
52 }

 

posted @ 2015-08-26 10:41  chengling  阅读(444)  评论(0编辑  收藏  举报