public class StreamDemo4 {
public static void main ( String [ ] args) {
InputStream inputStream = null ;
try {
inputStream = new FileInputStream ( "abc.txt" ) ;
int length = 0 ;
byte [ ] buffer = new byte [ 1024 ] ;
while ( ( length = inputStream. read ( buffer, 5 , 5 ) ) != - 1 ) {
System . out. println ( new String ( buffer, 5 , length) ) ;
}
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
try {
inputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
public class CopyFile {
public static void main ( String [ ] args) {
File src = new File ( "abc.txt" ) ;
File dest = new File ( "aaa.txt" ) ;
InputStream inputStream = null ;
OutputStream outputStream = null ;
try {
inputStream = new FileInputStream ( src) ;
outputStream = new FileOutputStream ( dest) ;
byte [ ] buffer = new byte [ 1024 ] ;
int length = 0 ;
while ( ( length = inputStream. read ( buffer) ) != - 1 ) {
outputStream. write ( buffer) ;
}
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
try {
outputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
try {
inputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
public class ReaderDemo3 {
public static void main ( String [ ] args) {
Reader reader = null ;
try {
reader = new FileReader ( "abc.txt" ) ;
int length = 0 ;
char [ ] chars = new char [ 1024 ] ;
while ( ( length = reader. read ( chars) ) != - 1 ) {
System . out. println ( new String ( chars, 0 , length) ) ;
}
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
try {
reader. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
public class WriterDemo {
public static void main ( String [ ] args) {
File file = new File ( "writer.txt" ) ;
Writer writer = null ;
try {
writer = new FileWriter ( file) ;
writer. write ( "www.mashibing.com" ) ;
writer. write ( "随便写点什么" ) ;
writer. flush ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
try {
writer. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}