file出现的意义即永久保存数据 file是对问文件及文件夹路径的抽象表示,file封装的对象路径可以存在也可以不存在 之所以封装file对象是为了使用其方法 路径可分为绝对路径:即从盘符开始的路径,相对路径:即相对于某个点来说 file方法
public class Test05 {
public static void main ( String [ ] args) throws IOException {
File file = new File ( "D:\\file\\x.txt" ) ;
boolean newFile = file. createNewFile ( ) ;
System . out. println ( newFile) ;
File file1 = new File ( "D:\\file\\ddd\\fff\\kkk" ) ;
boolean mkdirs = file1. mkdirs ( ) ;
System . out. println ( mkdirs) ;
File file2 = new File ( "D:\\file\\x.txt" ) ;
boolean delete = file2. delete ( ) ;
System . out. println ( delete) ;
File file3 = new File ( "D:\\file" ) ;
boolean delete1 = file3. delete ( ) ;
System . out. println ( delete1) ;
File file4 = new File ( "D:\\file\\a.txt" ) ;
boolean file10 = file4. isFile ( ) ;
System . out. println ( file) ;
File file5 = new File ( "D:\\file" ) ;
boolean directory = file5. isDirectory ( ) ;
System . out. println ( directory) ;
File file6 = new File ( "D:\\file\\c.txt" ) ;
boolean exists = file6. exists ( ) ;
System . out. println ( exists) ;
File file7 = new File ( "D:\\file\\b.txt" ) ;
String name = file7. getName ( ) ;
System . out. println ( name) ;
File file8 = new File ( "D:\\" ) ;
File [ ] files = file8. listFiles ( ) ;
for ( File file9 : files) {
System . out. println ( file) ;
}
}
}
IO流可分为输出流、输入流 (即操作文件内容) 字节流:可操作所有类型文件,音频.视频.图片等等 字符流:只能操作纯文本文件,即能用电脑自带记事本打开且能看懂的文件 字节输出入方法
public class Test06 {
public static void main ( String [ ] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream ( "D:\\file\\v.txt" , true ) ;
fileOutputStream. write ( 99 ) ;
byte [ ] bytes1 = "\r\n" . getBytes ( ) ;
fileOutputStream. write ( 94 ) ;
fileOutputStream. write ( 95 ) ;
byte [ ] bytes = { 1 , 4 , 6 , 7 , 8 , 9 , 5 } ;
fileOutputStream. write ( bytes) ;
fileOutputStream. write ( bytes, 0 , 3 ) ;
fileOutputStream. close ( ) ;
}
}
public class Test03 {
public static void main ( String [ ] args) {
FileOutputStream fileOutputStream= null ;
try {
fileOutputStream = new FileOutputStream ( "itzhangsong-inherit\\b.txt\\" ) ;
fileOutputStream. write ( 99 ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( fileOutputStream!= null ) {
try {
fileOutputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
public class Test07 {
public static void main ( String [ ] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream ( "D:\\file\\v.txt" ) ;
int read = fileInputStream. read ( ) ;
System . out. println ( read) ;
int b ;
while ( ( b= fileInputStream. read ( ) ) != - 1 ) {
System . out. println ( b) ;
}
FileInputStream fileInputStream1 = new FileInputStream ( "D:\\file\\v.txt" ) ;
FileOutputStream fileOutputStream = new FileOutputStream ( "D:\\file\\u.txt" ) ;
int c;
while ( ( b= fileInputStream. read ( ) ) != - 1 ) {
fileOutputStream. write ( b) ;
}
fileInputStream. close ( ) ;
fileOutputStream. close ( ) ;
}
}
public class Test03 {
public static void main ( String [ ] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream ( "D:\\file\\c.avi" ) ;
FileOutputStream fileOutputStream = new FileOutputStream ( "D:\\file\\d.avi" ) ;
byte [ ] bytes = new byte [ 1024 ] ;
int b ;
while ( ( b= fileInputStream. read ( bytes) ) != - 1 ) {
fileOutputStream. write ( bytes, 0 , b) ;
}
fileInputStream. close ( ) ;
fileOutputStream. close ( ) ;
}
}
ublic class Test04 {
public static void main ( String [ ] args) throws IOException {
BufferedInputStream bufferedInputStream = new BufferedInputStream ( new FileInputStream ( "D:\\file\\d.avi" ) ) ;
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream ( new FileOutputStream ( "D:\\file\\s.avi" ) ) ;
int b;
while ( ( b= bufferedInputStream. read ( ) ) != - 1 ) {
bufferedOutputStream. write ( b) ;
}
bufferedInputStream. close ( ) ;
bufferedOutputStream. close ( ) ;
}
}