一、文件操作类
1.文件的创建与删除
File类不能操作文件的内容,只能进行创建删除等对文件本身的操作 File类提供一个常量 public static final String separator,表示分隔符
package 文件操作类;
import java. io. File;
public class FileDemo {
public static void main ( String[ ] args) throws Exception {
File file = new File ( "e:" + File. separator+ "aaa.txt" ) ;
if ( file. exists ( ) ) { file. delete ( ) ;
System. out. println ( "the new file has been deleted!" ) ; }
else { file. createNewFile ( ) ;
System. out. println ( "the new file has been created!" ) ; }
}
}
文件目录不存在时,需要先判断目录的存在性,再进行创建目录 getParent( ).mkdir( )只能创建一级目录 getParent( ).mkdirs( )创建多级目录
package 文件操作类;
import java. io. File;
public class FileDemo1 {
public static void main ( String[ ] args) throws Exception {
File file = new File ( "e:" + File. separator+ "demo" + File. separator+ "aaa.txt" ) ;
if ( ! file. getParentFile ( ) . exists ( ) )
{
file. getParentFile ( ) . mkdir ( ) ;
}
if ( file. exists ( ) ) { file. delete ( ) ;
System. out. println ( "文件已删除!!!" ) ;
System. out. println ( ) ; }
else { file. createNewFile ( ) ;
System. out. println ( "文件已创建!!!" ) ; }
}
}
package 文件操作类;
import java. io. File;
public class FileDemo1 {
public static void main ( String[ ] args) throws Exception {
File file1 = new File ( "e:" + File. separator+ "demo" + File. separator+ "huicoker" + File. separator+ "test" + File. separator+ "aaa.txt" ) ;
if ( ! file1. getParentFile ( ) . exists ( ) )
{
file1. getParentFile ( ) . mkdirs ( ) ;
}
if ( file1. exists ( ) ) { file1. delete ( ) ;
System. out. println ( "文件已删除!!!" ) ;
System. out. println ( ) ; }
else { file1. createNewFile ( ) ;
System. out. println ( "文件已创建!!!" ) ; }
}
}
2.文件的信息的输出
public long length() — 获取文件的字节大小 public long lastModified() —获取文件最近的一次修改时间
package 文件操作类;
import java. io. File;
import java. math. BigDecimal;
import java. text. SimpleDateFormat;
import java. util. Date;
public class FileDemo2 {
public static void main ( String[ ] args) {
File file = new File ( "E:" + File. separator+ "butterfly.jpg" ) ;
File file1 = new File ( "E:" + File. separator+ "demo" ) ;
System. out. println ( file1. getPath ( ) ) ;
if ( file. exists ( ) | file1. exists ( ) ) {
System. out. println ( "是否是文件:" + file. isFile ( ) ) ;
System. out. println ( "是否是目录:" + file. isDirectory ( ) ) ;
System. out. println ( "是否是目录:" + file1. isDirectory ( ) ) ;
System. out. println ( "文件大小:" + ( new BigDecimal ( ( double ) file. length ( ) / 1024 / 1024 ) . divide ( new BigDecimal ( 1 ) , 2 , BigDecimal. ROUND_HALF_UP) ) + "M" ) ;
System. out. println ( "最近的一次修改时间:" + new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) . format ( new Date ( file. lastModified ( ) ) ) ) ;
}
}
}
3.列出目录下的文件
public String[] list()—>列出目录下的子目录及文件的名字 public File[] listFiles()—>列出目录包含路径,以File类对象的形式列出
package 文件操作类;
import java. io. File;
public class FileDemo3 {
public static void main ( String[ ] args) throws Exception{
File file = new File ( "E:" ) ;
if ( file. isDirectory ( ) ) {
String[ ] result = file. list ( ) ;
for ( int i = 0 ; i < result. length; i++ ) {
System. out. println ( result[ i] ) ;
}
}
File result1[ ] = file. listFiles ( ) ;
for ( int i = 0 ; i < result1. length; i++ ) {
System. out. println ( result1[ i] ) ;
}
}
}
package 文件操作类;
import java. io. File;
public class FileDemo3 {
public static void main ( String[ ] args) throws Exception{
File file = new File ( "E:" + File. separator+ "Documents" ) ;
print ( file) ;
}
public static void print ( File file)
{
if ( file. isDirectory ( ) ) {
File result[ ] = file. listFiles ( ) ;
if ( result != null) {
for ( int i = 0 ; i < result. length; i++ ) {
print ( result[ i] ) ;
}
}
}
System. out. println ( file) ;
}
}
二、字节流
1.字节输入流
字符输入流,从文件中读出来
读取单个字节,读到结尾返回-1,while循环: public abstract int read() throws IOException 读取的数据保存在字符数组中,返回读出的数据的长度: public int read(byte b[]) throws IOException
package 文件操作类;
import java. io. File;
import java. io. FileInputStream;
import java. io. InputStream;
public class FileInputStreamDemo {
public static void main ( String[ ] args) throws Exception{
File file = new File ( "E:" + File. separator+ "bbb.txt" ) ;
if ( file. exists ( ) ) {
InputStream in = new FileInputStream ( file) ;
byte data[ ] = new byte [ 1024 ] ;
int len = in. read ( data) ;
in. close ( ) ;
System. out. println ( new String ( data, 0 , len) ) ;
int foot = 0 ;
int temp = 0 ;
while ( ( temp= in. read ( ) ) != - 1 )
{
data[ foot++ ] = ( byte ) temp;
}
in. close ( ) ;
System. out. println ( new String ( data, 0 , foot) ) ;
}
}
}
2.字节输出流
OutputStream本身是抽象类,用子类FileOutputStream实例化 字节输出流,将数据写进文件中,创建或覆盖已有的文件
以字节数组全部输出:public void write(byte b[]) throws IOException 以字节数组部分输出: public void write(byte b[], int off, int len) throws IOException FileOutputStream(file,true),加上true,可以实现文件的续写,不会覆盖 "\r\n"实现换行 文件使用完毕关闭,使用close( )
package 文件操作类;
import java. io. File;
import java. io. FileOutputStream;
import java. io. OutputStream;
public class FileOutputStreamDemo {
public static void main ( String[ ] args) throws Exception {
File file = new File ( "E:" + File. separator + "aaa.txt" ) ;
if ( ! file. exists ( ) ) file. createNewFile ( ) ;
OutputStream out = new FileOutputStream ( file, true ) ;
String str = "1 2 3 4 5 6 " + "\r\n" ;
String str1 = "huicoker---" + "\r\n" ;
byte data[ ] = str. getBytes ( ) ;
byte data1[ ] = str1. getBytes ( ) ;
out. write ( data) ;
for ( int i = 0 ; i < data1. length; i++ ) {
out. write ( data1[ i] ) ;
}
out. write ( data, 3 , 9 ) ;
out. close ( ) ;
}
}
三、字符流
1.字符输入流
Reader类需要FileReader来实现 字符流需要缓冲区处理,用完后一定要加close()或flush( ) 读取数据到char类型的数组中:public int read(char cbuf[]) throws IOException
package 文件操作类;
import java. io. File;
import java. io. FileReader;
import java. io. Reader;
public class FileReaderDemo {
public static void main ( String[ ] args) throws Exception{
File file = new File ( "E:" + File. separator+ "ddd.txt" ) ;
if ( file. exists ( ) )
{
Reader reader = new FileReader ( file) ;
char data[ ] = new char [ 1024 ] ;
int len = reader. read ( data) ;
reader. close ( ) ;
System. out. println ( new String ( data, 0 , len) ) ;
}
}
}
2.字符输出流
Writer类需要FileWriter类来实现 可以输出字符串: public void write(String str) throws IOException 可以输出char类型的数组: public void write(char cbuf[]) throws IOException 增加append()追加操作: public Writer append(CharSequence csq) throws IOException
package 文件操作类;
import java. io. File;
import java. io. FileWriter;
import java. io. Writer;
public class FileWriterDemo {
public static void main ( String[ ] args) throws Exception{
File file = new File ( "E:" + File. separator+ "ccc.txt" ) ;
if ( ! file. exists ( ) ) file. createNewFile ( ) ;
Writer writer = new FileWriter ( file) ;
String str = "it has been a long time!!!" ;
char data[ ] = new char [ ] { '中' , '国' } ;
writer. write ( str) ;
writer. append ( "\r\n" ) ;
writer. write ( data) ;
writer. close ( ) ;
}
}
3.字节流转化为字符流
public class OutputStreamWriter extends Writer public OutputStreamWriter(OutputStream out)
package 文件操作类;
import java. io. *;
public class ChangeTo {
public static void main ( String[ ] args) throws Exception{
File file = new File ( "E:" + File. separator+ "eee.txt" ) ;
if ( ! file. exists ( ) ) file. createNewFile ( ) ;
OutputStream outputStream = new FileOutputStream ( file) ;
Writer writer = new OutputStreamWriter ( outputStream) ;
writer. write ( "hello world!!!" ) ;
writer. close ( ) ;
}
}
四、实现文件的复制
package 文件操作类;
import java. io. *;
public class CopyDemo {
public static void main ( String[ ] args) throws Exception{
File infile = new File ( "E:" + File. separator+ "butterfly.jpg" ) ;
File outfile = new File ( "E:" + File. separator+ "hello.jpg" ) ;
if ( ! outfile. exists ( ) ) outfile. createNewFile ( ) ;
InputStream inputStream = new FileInputStream ( infile) ;
OutputStream outputStream = new FileOutputStream ( outfile) ;
int temp = 0 ;
byte data[ ] = new byte [ 1024 ] ;
while ( ( temp = inputStream. read ( data) ) != - 1 )
{
outputStream. write ( data, 0 , temp) ;
}
inputStream. close ( ) ;
outputStream. close ( ) ;
}
}