IO流可以分为两部分:字节流和字符流。
1)以字节流的方式读取文件:
public static void readByFileInputSteam(File file){
InputStream inputSteam= null;
byte[] tempByte = new byte[1024];
int byteRead = 0;
try {
inputSteam = new FileInputStream(file);
while ((byteRead = inputSteam.read(tempByte ))!=-1){
System.out.println( byteRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
2)以字节流的方式写入类容的FileOutputStream:
public static void writeByFileOutputSteam(File file, String content){
FileOutputStream fileOutputStream =null;
try {
fileOutputStream = new FileOutputStream(file, true);
byte [] tempBytes = content.getBytes();
fileOutputStream.write(tempBytes);
fileOutputStream.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(fileOutputStream!=null){
try{
fileOutputStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
3)以字符流的方式写入文件:
public static void writeByBufferWrite(File file,String content){
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new PrintWriter(file));
bufferedWriter.write(content);
bufferedWriter.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if(bufferedWriter!=null){
try{
bufferedWriter.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
4)随机访问读取文件:
public static void randomRead(File file, int pointer){
RandomAccessFile randomAccessFile=null;
try {
randomAccessFile = new RandomAccessFile(file, "r");
byte [ ] bytes = new byte[1024];
int hasRead = 0;
try {
while((hasRead = randomAccessFile.read())!=-1){
System.out.println(new String(bytes, 0 , hasRead));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(randomAccessFile!=null){
try {
randomAccessFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
5)随机访问写文件:
public static void randomWrite(File file, String appendContent, long seek){
RandomAccessFile randomAccessFile=null;
try {
randomAccessFile = new RandomAccessFile(file, "w");
try {
randomAccessFile.seek(randomAccessFile.length());
randomAccessFile.write(appendContent.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
6)随机访问读写文件:
public static void insertByRandomccessFile(File file, long pointer, String InsertContentAction){
try{
File tempFile = file.createTempFile("temp", null);
tempFile.deleteOnExit();
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
FileInputStream fileInputStream = new FileInputStream(tempFile);
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
randomAccessFile.seek(pointer);
byte [] bytes = new byte[1024];
int hasRead= 0;
while((hasRead = randomAccessFile.read(bytes))!=-1){
fileOutputStream.write(bytes, 0,hasRead);
}
randomAccessFile.write(InsertContentAction.getBytes());
while ((hasRead = randomAccessFile.read())!=-1){
fileOutputStream.write(bytes,0,hasRead);
}
randomAccessFile.close();
fileOutputStream.close();
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
7)类存流:
public static void main(String[] args) {
ranStream();
}
public static void ranStream(){
String string = "hello work";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(string.getBytes());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int hasRead=0;
while((hasRead= byteArrayInputStream.read())!=-1){
char c= (char)hasRead;
byteArrayOutputStream.write(Character.toUpperCase(c));
}
String result= byteArrayOutputStream.toString();
String result= new String(byteArrayOutputStream.toByteArray(),0,string.length());
System.out.println(result);
}
8)对象流读取文件:
public static Message objectReadStream(File file){
ObjectInputStream objectInputStream = null;
Message message = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(file));
try {
message = (Message)objectInputStream.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(objectInputStream!=null){
try {
objectInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return message;
}
9)对象流写文件:
public static void objectWriterSteam(File file , Message message){
ObjectOutputStream objectOutputStream =null;
try{
objectOutputStream = new ObjectOutputStream(new FileOutputStream(file ));
objectOutputStream.writeObject(message);
objectOutputStream.flush();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(ClassCastException e){
e.printStackTrace();
}finally{
if(objectOutputStream!=null){
try{
objectOutputStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
1)以字节流的方式读取文件:
public static void readByFileInputSteam(File file){
InputStream inputSteam= null;
byte[] tempByte = new byte[1024];
int byteRead = 0;
try {
inputSteam = new FileInputStream(file);
while ((byteRead = inputSteam.read(tempByte ))!=-1){
System.out.println( byteRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
2)以字节流的方式写入类容的FileOutputStream:
public static void writeByFileOutputSteam(File file, String content){
FileOutputStream fileOutputStream =null;
try {
fileOutputStream = new FileOutputStream(file, true);
byte [] tempBytes = content.getBytes();
fileOutputStream.write(tempBytes);
fileOutputStream.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(fileOutputStream!=null){
try{
fileOutputStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
3)以字符流的方式写入文件:
public static void writeByBufferWrite(File file,String content){
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new PrintWriter(file));
bufferedWriter.write(content);
bufferedWriter.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if(bufferedWriter!=null){
try{
bufferedWriter.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
4)随机访问读取文件:
public static void randomRead(File file, int pointer){
RandomAccessFile randomAccessFile=null;
try {
randomAccessFile = new RandomAccessFile(file, "r");
byte [ ] bytes = new byte[1024];
int hasRead = 0;
try {
while((hasRead = randomAccessFile.read())!=-1){
System.out.println(new String(bytes, 0 , hasRead));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(randomAccessFile!=null){
try {
randomAccessFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
5)随机访问写文件:
public static void randomWrite(File file, String appendContent, long seek){
RandomAccessFile randomAccessFile=null;
try {
randomAccessFile = new RandomAccessFile(file, "w");
try {
randomAccessFile.seek(randomAccessFile.length());
randomAccessFile.write(appendContent.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
6)随机访问读写文件:
public static void insertByRandomccessFile(File file, long pointer, String InsertContentAction){
try{
File tempFile = file.createTempFile("temp", null);
tempFile.deleteOnExit();
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
FileInputStream fileInputStream = new FileInputStream(tempFile);
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
randomAccessFile.seek(pointer);
byte [] bytes = new byte[1024];
int hasRead= 0;
while((hasRead = randomAccessFile.read(bytes))!=-1){
fileOutputStream.write(bytes, 0,hasRead);
}
randomAccessFile.write(InsertContentAction.getBytes());
while ((hasRead = randomAccessFile.read())!=-1){
fileOutputStream.write(bytes,0,hasRead);
}
randomAccessFile.close();
fileOutputStream.close();
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
7)类存流:
public static void main(String[] args) {
ranStream();
}
public static void ranStream(){
String string = "hello work";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(string.getBytes());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int hasRead=0;
while((hasRead= byteArrayInputStream.read())!=-1){
char c= (char)hasRead;
byteArrayOutputStream.write(Character.toUpperCase(c));
}
String result= byteArrayOutputStream.toString();
String result= new String(byteArrayOutputStream.toByteArray(),0,string.length());
System.out.println(result);
}
8)对象流读取文件:
public static Message objectReadStream(File file){
ObjectInputStream objectInputStream = null;
Message message = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(file));
try {
message = (Message)objectInputStream.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(objectInputStream!=null){
try {
objectInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return message;
}
9)对象流写文件:
public static void objectWriterSteam(File file , Message message){
ObjectOutputStream objectOutputStream =null;
try{
objectOutputStream = new ObjectOutputStream(new FileOutputStream(file ));
objectOutputStream.writeObject(message);
objectOutputStream.flush();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(ClassCastException e){
e.printStackTrace();
}finally{
if(objectOutputStream!=null){
try{
objectOutputStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}