자바 중 8 가지 IO 조작
package com.mengya.TestIO;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
public class TestIO {
/**
* Java 8 IO
*/
/*
*
*/
public void CreateFile(){
File f=new File("e:\\io.txt");
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* FileOutputStream
*/
public void set1(){
try {
FileOutputStream f=new FileOutputStream("e:\\io.txt");
String str=" ";
byte[] b=str.getBytes();
try {
f.write(b);
f.flush();
f.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/*
* FileInputStream
*/
public void get1(){
try {
FileInputStream f=new FileInputStream("e:\\io.txt");
byte[] b=new byte[200];
try {
int n=f.read(b);
String str=new String(b,0,n);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/*
*
*/
public void set2(){
try {
FileOutputStream f=new FileOutputStream("e:\\io.txt",true);// true , true
BufferedOutputStream ff=new BufferedOutputStream(f);
String str=new String(" ");
try {
ff.write(str.getBytes());
ff.flush();
ff.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void get2(){
try {
FileInputStream f=new FileInputStream("e:\\io.txt");
BufferedInputStream ff=new BufferedInputStream(f);
byte[] b=new byte[200];
try {
int n = ff.read(b);
String str=new String(b,0,n);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/*
*
*/
public void set3(){
try {
FileWriter f=new FileWriter("e:\\io.txt",true);
f.write("\r
");//\r
f.flush();
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void get3(){
try {
FileReader f=new FileReader("e:\\io.txt");
char[] c=new char[200];
try {
int n = f.read(c);
String str=new String(c,0,n);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void set4(){
try {
FileWriter f=new FileWriter("e:\\io.txt",true);
BufferedWriter ff=new BufferedWriter(f);
ff.write(" ");
ff.flush();
ff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void get4(){
try {
FileReader f=new FileReader("e:\\io.txt");
BufferedReader ff=new BufferedReader(f);
char[] c=new char[200];
int n;
try {
n = ff.read(c);
String str=new String(c,0,n);
System.out.println(str);
ff.close();
f.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/*
*
*/
public void h1(){
try {
FileOutputStream f=new FileOutputStream("e:\\io.txt",true);
OutputStreamWriter ff=new OutputStreamWriter(f);
try {
ff.write("\r
");
ff.flush();
ff.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void h2(){
try {
FileInputStream f=new FileInputStream("e:\\io.txt");
InputStreamReader ff=new InputStreamReader(f);
char[] c=new char[200];
int n;
try {
n = ff.read(c);
String str=new String(c,0,n);
System.out.println(str);
ff.close();
f.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/*
*
*/
public void hh(){
try {
FileReader f=new FileReader("e:\\io.txt");
BufferedReader ff=new BufferedReader(f);
while(ff.ready()){
System.out.println(ff.readLine());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TestIO test=new TestIO();
test.CreateFile();
//test.set1();
//test.get1();
//test.set2();
//test.get2();
//test.set3();
//test.get3();
//test.set4();
//test.get4();
//test.h1();
//test.h2();
test.hh();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.