IO流

来源:互联网 发布:北欧沙发品牌 知乎 编辑:程序博客网 时间:2024/06/05 06:31
package cn.Day1001;import java.io.FileInputStream;import java.io.IOException;public class IO02 {
package cn.Day1001;
package cn.Day1001;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class IO07 {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubInputStream is = new FileInputStream("E:/北大青鸟.png");DataInputStream dis = new DataInputStream(is);// copy到目标路径。OutputStream os = new FileOutputStream("F:/北大青鸟1.png");DataOutputStream dos = new DataOutputStream(os);byte[] bytes = new byte[1024];int data;while ((data = dis.read(bytes)) != -1) {dos.write(bytes, 0, data);}dos.close();os.close();dis.close();is.close();}}

import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.io.Reader;public class IO05 {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubReader reader = new FileReader("E:/book.txt");BufferedReader br = new BufferedReader(reader);String line;while ((line = br.readLine()) != null) {System.out.println(line);}br.close();reader.close();}}

package cn.Day1001;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.io.Writer;public class IO06 {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubWriter writer = new FileWriter("E:/book.txt");BufferedWriter bw = new BufferedWriter(writer);String words = "已经晚了";bw.write(words);bw.close();writer.close();}}

/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubFileInputStream fis = new FileInputStream("E:/book.txt");// 定义一个缓冲区byte[]byte[] bytes = new byte[1024];int data;while ((data = fis.read(bytes)) != -1) {// 有数据可读String type = new String(bytes, 0, data);System.out.println(type);fis.close();}}}

package cn.Day1001;import java.io.FileWriter;import java.io.IOException;public class IO03 {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubFileWriter writer = new FileWriter("E:/book.txt");String words = "她就像是一道疤!";writer.write(words);writer.close();}}

package cn.Day1001;import java.io.FileReader;import java.io.IOException;public class IO04 {/** * @param args * @throws IOException */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubFileReader reader = new FileReader("E:/book.txt");char[] chars = new char[1024];int data;while ((data = reader.read(chars)) != -1) {String temp = new String(chars, 0, data);System.out.println(temp);}reader.close();}}

0 0
原创粉丝点击