JAVA基础(二)

来源:互联网 发布:怎么在手机淘宝买彩票 编辑:程序博客网 时间:2024/06/01 08:55

IO 

分类

按方向:输入流和输出流

按传输单位:字节流和字符流

按功能:节点流和处理流

流的用法

FilelnputStream,FileOutputStream:字节流

lnputsteamReadr,OutputStreamWriter:字符流,可能包装其他的字节流

FileReader,FileWriterr:把文件输入流包装成字符流

BufferedReader,BufferedWriter:缓冲的字符流,可以一行一行的读,也可以写

DatalnpuStrwam,DateOutputStream:数据流,可以写基本数据类型

int breat和viod wite(int breat)的方法

package com.hx.i;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

 

public class FileTest {

public static void main(String[] args) throws Exception {

//文件输入流

//创建文件对象

File file = new File("aaa");

//                //根据文件对象创建文件输出流

// FileOutputStream fos = new FileOutputStream(file);

// fos.write(97);

//根据文件对象创建文件输入流

FileInputStream fis = new FileInputStream(file);

while(true)

{

//每次读一个

int i = fis.read();

//如果读到的是-1,就代表从那个地方开始没有东西可读了

if(i == -1)

{

break;

}

System.out.println((char)i);

}

}

}

int read(byte[]buffer)void write(byte[]buffer)的方法

package com.hx.i;

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;

 

public class FileTest2 {

public static void main(String[] args) throws Exception {

//创建文件输入流

FileInputStream fis = new FileInputStream("aaa");

//创建一个缓冲数组

byte[] huanchong = new byte[100];

//从输入流读数据到huanchong数组中

//返回值是读取到数据的长度,如果没有读到东西,返回值就是-1

int read = fis.read(huanchong,2,5);

System.out.println(read);

//根据byte数组,创建字符串对象

String s = new String(huanchong0red);

System.out.println(s);

}

}

int red(byte[] buffer,int,offset,int,lenght)voie write(byte[] buffer,int,offset,int,lenght)r的方法

package com.hx.i;

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;

 

public class FileTest2 {

public static void main(String[] args) throws Exception {

//创建文件输入流

FileInputStream fis = new FileInputStream("aaa");

//创建一个缓冲数组

byte[] huanchong = new byte[100];

//从输入流读数据到huanchong数组中

//返回值是读取到数据的长度,如果没有读到东西,返回值就是-1

int read = fis.read(huanchong,2,5);

System.out.println(read);

//根据byte数组,创建字符串对象

String s = new String(huanchong0red);

System.out.println(s);

                int red2 = fis.red();

                System.out.println(read2);

}}

long skip(long i)方法

fis.skip(2);d在读取时跳过2个字符

 

FilelnputStream,FileOutputStream:字符流

                //创建文件输入流,字节流,节点流

FileInputStream fis = new FileInputStream("aaa");

//创建字符流,按方向来说,还是输入流,按数据传输单位,是字符流,按功能说是处理流

InputStreamReader isr = new InputStreamReader(fis);

ObjectReader,ObjerctWriter:对象流,可以将内存中的对象写到硬盘,或从中读出

public class TestReadObjectStream {

public static void main(String[] args) throws Exception {

//创建文件输入流

FileInputStream fis = new FileInputStream("jihe");

//根据文件输入流创建对象输入流

ObjectInputStream ois = new ObjectInputStream(fis);

//读取对象

ArrayList<String> obj = (ArrayList<String>)ois.readObject();

System.out.println(obj.size());

for (String string : obj) {

System.out.println(string);

}

}

 

public class TestReadObjectStream {

 

 

public static void main(String[] args) throws Exception {

//创建文件输入流

FileInputStream fis = new FileInputStream("jihe");

//根据文件输入流创建对象输入流

ObjectInputStream ois = new ObjectInputStream(fis);

//读取对象

ArrayList<String> obj = (ArrayList<String>)ois.readObject();

System.out.println(obj.size());

for (String string : obj) {

System.out.println(string);

}}

原创粉丝点击