目录
I/O流
ByteArrayOutputStream
ObjectInputStream
ObjectOutputStream
InputStreamReader
OutputStreamWriter
InputStream?
?OutputStream
CharArrayRead
CharArraryWrite
I/O流
有很多重要的知识点,例如字节流里的InputStream ,OutputStream,字符流里的Reader,Writer这只是主要的两个题目,里面延伸的也有很多知识点
例如InputStream ,OutputStream里的会有FileInputStream,FileOutputStream;ByteArraryInputStream,ByteOutputStreamd;等许多知识点
public class ByteArrayInputSteamDemo {
public static void main(String[] args) throws IOException {
String str = "www.baidu.com 百度";
byte []buffer = str.getBytes();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer);
int length = 0;
//缓存区
byte []buffer1 = new byte[1024];
while((length = byteArrayInputStream.read(buffer1))!=-1){
System.out.println(new String (buffer1,0,length));
}
try {
byteArrayInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ByteArrayOutputStream
public class ByteArrayOutputSteamDemo {
public static void main(String[] args) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write(58);
byteArrayOutputStream.write("12345".getBytes(),1,3);
try {
byteArrayOutputStream.write("www.baidu 百度".getBytes());
System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
}
}
}
}
public class ObjectInputStreamDemo {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
ObjectInputStream objectInputStream = null;
try {
fileInputStream = new FileInputStream("a.txt");
objectInputStream = new ObjectInputStream(fileInputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ObjectOutputStream
public class ObjectOutputStreamDemo {
public static void main(String[] args) {
FileOutputStream fileOutputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
fileOutputStream = new FileOutputStream("a.txt");
objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.write("www.cz.com".getBytes());
objectOutputStream.flush();
objectOutputStream.writeUTF("wwww.sdsdvsv.com.呵呵");
objectOutputStream.flush();
objectOutputStream.writeObject(new person("凉诀城",18,520));
objectOutputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
objectOutputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = null;
Reader reader= null;
try {
fileInputStream = new FileInputStream("b2.txt");
reader = new InputStreamReader(fileInputStream,"gbk");
int length = 2;
char[]chars = new char[1024];
while((length = reader.read(chars))!=-1){
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
OutputStreamWriter
public class OutputStreamWriterDemo {
public static void main(String[] args) {
FileOutputStream fileOutputStream = null;
OutputStreamWriter outputStreamWriter = null;
try {
fileOutputStream = new FileOutputStream("a2.txt");
outputStreamWriter = new OutputStreamWriter(fileOutputStream);
outputStreamWriter.write(520);
outputStreamWriter.write("qiamwei");
outputStreamWriter.write(250);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class InputStreamDemo {
public static void main(String[] args) {
InputStream inputStream = null;
try {
//创建流对象
inputStream = new FileInputStream("a1.txt");
//传输数据
int read = inputStream.read();
System.out.println((char)read);
read = inputStream.read();
System.out.println((char)read);
read = inputStream.read();
System.out.println((char)read);
read = inputStream.read();
System.out.println((char)read);
read = inputStream.read();
System.out.println((char)read);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
?OutputStream
public class OutputStreamDemo {
public static void main(String[] args) {
File file = new File("a2.txt");
OutputStream outputStream = null;
try {
//创建流对象
outputStream = new FileOutputStream(file,true);
//传输数据
outputStream.write(98);
outputStream.write("www.baidu.com百度".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流对象
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
CharArrayRead
public class CharArrayReadDemo {
public static void main(String[] args) {
String str = "www.baidu.com百度";
char[] chars = str.toCharArray();
CharArrayReader charArrayReader = new CharArrayReader(chars);
int length = 0;
char[] buffer = new char[1024];
try {
while (!((length = charArrayReader.read(buffer)) != -1)) break;
// System.out.println(new String (chars,0,length));
System.out.println(buffer);
} catch (IOException e) {
e.printStackTrace();
} finally {
charArrayReader.close();
}
}
}
CharArraryWrite
public class CharArrayWriteDemo {
public static void main(String[] args) {
CharArrayWriter charArrayWriter = null;
try {
charArrayWriter =new CharArrayWriter();
charArrayWriter.write(100);
charArrayWriter.write("www.alibaba");
charArrayWriter.append("A").append("s");
} catch (IOException e) {
e.printStackTrace();
}finally {
charArrayWriter.close();
}
}
}
当然I/O流里的知识不止这些,我只是简单的例举了几个而已。
|