No.1
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest01 {
public static void main(String[] args) {
File src = new File("abc.txt");
InputStream is =null;
try {
is =new FileInputStream(src);
int temp ;
while((temp=is.read())!=-1) {
System.out.println((char)temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(null!=is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest02 {
public static void main(String[] args){
File src = new File("abc.txt");
InputStream is = null;
try {
is = new FileInputStream(src);
byte[] flush = new byte[1024*10];
int len = -1;
while ((len=is.read(flush))!=-1){
String str = new String(flush,0,len);
System.out.println(str);
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try {
if(null!=is){
is.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class IOTest03 {
public static void main(String[] args){
File dest = new File("dest.txt");
OutputStream os = null;
try {
os = new FileOutputStream(dest,true);
String msg = "IO is so easy\r\n";
byte[] datas = msg.getBytes();
os.write(datas,0,datas.length);
os.flush();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if (null != os){
os.close();
}
}catch(Exception e){
} }
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class IOTest04 {
public static void main(String[] args){
File src = new File("abc.txt");
Reader reader = null;
try {
reader = new FileReader(src);
char[] flush = new char[1024];
int len = -1;
while((len=reader.read(flush))!=-1){
String str = new String(flush,0,len);
System.out.println(str);
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally{
try {
if (null!=reader){
reader.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class IOTest05 {
public static void main(String[] args){
File dest = new File("dest.txt");
Writer writer = null;
try {
writer = new FileWroter(dest);
writer.append("IO is so easy\r\n").append("屌丝逆袭");
writer.flush();
}catch(FileNotFoundException e){
e.printStrackTrace();
}catch(IOException e){
e.printStrackTrace();
}finally{
try {
if (null!=writer){
writer.close();
}
}catch(Exception e){
}
}
}
}
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest06 {
public static void main(String[] args){
byte[] src = "talk is cheap show me the code".getBytes();
InputStream is = null;
try {
is = new ByteArrayInputStream(src);
byte[] flush = new byte[5];
int len = -1;
while((len=is.read(flush))!=-1){
String str = new String(flush,0,len);
System.out.println(str);
}
}catch(IOException e){
e.printStrackTrace();
}finally{
try {
if(null!=is){
is.close();
}
}catch(IOException e){
e.printStrackTrace();
}
}
}
}
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest07 {
public static void main(String[] args) {
byte[] src = "talk is cheap show me the code".getBytes();
InputStream is =null;
try {
is =new ByteArrayInputStream(src);
byte[] flush = new byte[5];
int len = -1;
while((len=is.read(flush))!=-1) {
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(null!=is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class IOTest08 {
public static void main(String[] args) {
byte[] dest =null;
ByteArrayOutputStream baos =null;
try {
baos = new ByteArrayOutputStream();
String msg ="show me the code";
byte[] datas =msg.getBytes();
baos.write(datas,0,datas.length);
baos.flush();
dest = baos.toByteArray();
System.out.println(dest.length +"-->"+new String(dest,0,baos.size()));
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (null != baos) {
baos.close();
}
} catch (Exception e) {
}
}
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IOTest09 {
public static void main(String[] args) {
byte[] datas = fileToByteArray("p.png");
System.out.println(datas.length);
byteArrayToFile(datas,"p-byte.png");
}
public static byte[] fileToByteArray(String filePath) {
File src = new File(filePath);
byte[] dest =null;
InputStream is =null;
ByteArrayOutputStream baos =null;
try {
is =new FileInputStream(src);
baos = new ByteArrayOutputStream();
byte[] flush = new byte[1024*10];
int len = -1;
while((len=is.read(flush))!=-1) {
baos.write(flush,0,len);
}
baos.flush();
return baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(null!=is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void byteArrayToFile(byte[] src,String filePath) {
File dest = new File(filePath);
InputStream is =null;
OutputStream os =null;
try {
is =new ByteArrayInputStream(src);
os = new FileOutputStream(dest);
byte[] flush = new byte[5];
int len = -1;
while((len=is.read(flush))!=-1) {
os.write(flush,0,len);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
}
}
}
}