JAVA——IO

I/O是input/output的简写

java.io包
File
RandomAccessFile
InputStream/OutputStream
FileInputStream/FileOutputStream
BufferedInputStream/BufferedOutputStream
ObjectInputStream/ObjectOutputStream
Reader/Writer
InputStreamReader/OutputStreamWriter
BufferReader/BufferedWriter

File
封装磁盘路径字符串
11文件路径
2文件夹路径
3不存在的路径


JAVA——IO_第1张图片
File

可以获得文件或文件夹属性
length()文件字节量,对文件夹无效
lastModified()最后修改时间,毫秒值
getName()文件名
getParent()父目录
getAbsolutePath()完整路径
exitsts()是否存在
isFile()是否是文件
isDirectory()是否是文件夹

public class TestFile {
    public static void main(String[] args) {
        String path;
        //path="D:\\android\\HelloApp\\AndroidManifest.xml";//存在的文件
        path="D:\\ant\\apache-ant-1.9.13-bin.zip";//存在的文件夹
        //path="x:\\ff\\aa\\bb";//不存在的路径
        File file=new File(path);//路径封装成File对象而已
        System.out.println(file.getAbsolutePath());//完整路径
        System.out.println(file.exists());//是否存在
        System.out.println(file.length()+"kb");//字节量,目录(文件夹)无效
        System.out.println(file.isFile());//是否是文件
        System.out.println(file.isDirectory());//是否是文件夹
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String lastModify=sdf.format(file.lastModified());//最后修改时间
        System.out.println(lastModify);
        
    }
}

运行结果

D:\ant\apache-ant-1.9.13-bin.zip
true
8718291kb
true
false
2018-07-17 23:57:42

可以创建、删除文件或文件夹
createNewFile()创建文件
mkdirs()创建多层文件夹
delete()删除文件,或空文件夹
File.createTempFile()在系统临时目录,创建临时文件

对文件夹进行列表
list()
文件、文件夹名称字符串数组String[]
listFiles()
文件、文件夹的封装对象数组File[]

public class TestFile {
    public static void main(String[] args) {
        File dir=new File("C:\\Program Files\\UNP\\Logs");
        /**
         * 如果目录不存在,或目录无权访问
         * 会得到null值
         */
        String[] names = dir.list();
        File[] files = dir.listFiles();
        System.out.println(Arrays.toString(names));
        System.out.println(Arrays.toString(files));
        for (String n : names) {
            System.out.println(n);
        }
        System.out.println("====================");
        for (File f : files) {
            System.out.println(f.getName()+"-"+f.length());
        }
    }
}

运行结果

[UniversalNotificationPlatform.001.etl, UniversalNotificationPlatform.002.etl, U
niversalNotificationPlatform.003.etl, UniversalNotificationPlatform.004.etl, Uni
versalNotificationPlatform.005.etl, UniversalNotificationPlatform.006.etl, Unive
rsalNotificationPlatform.007.etl, UniversalNotificationPlatform.008.etl, Univers
alNotificationPlatform.009.etl, UniversalNotificationPlatform.010.etl, Universal
NotificationPlatform.011.etl, UniversalNotificationPlatform.012.etl, UniversalNo
tificationPlatform.013.etl, UniversalNotificationPlatform.014.etl, UniversalNoti
ficationPlatform.015.etl, UniversalNotificationPlatform.016.etl, UniversalNotifi
cationPlatform.017.etl, UniversalNotificationPlatform.018.etl, UniversalNotifica
tionPlatform.019.etl, UniversalNotificationPlatform.020.etl, UniversalNotificati
onPlatform.021.etl, UniversalNotificationPlatform.022.etl, UniversalNotification
Platform.023.etl, UniversalNotificationPlatform.024.etl, UniversalNotificationPl
atform.025.etl, UniversalNotificationPlatform.026.etl, UniversalNotificationPlat
form.027.etl, UniversalNotificationPlatform.028.etl, UniversalNotificationPlatfo
rm.029.etl, UniversalNotificationPlatform.030.etl, UniversalNotificationPlatform
.031.etl, UniversalNotificationPlatform.032.etl, UniversalNotificationPlatform.0
33.etl, UniversalNotificationPlatform.034.etl, UniversalNotificationPlatform.035
.etl, UniversalNotificationPlatform.036.etl, UniversalNotificationPlatform.037.e
tl, UniversalNotificationPlatform.038.etl, UniversalNotificationPlatform.039.etl
, UniversalNotificationPlatform.040.etl, UniversalNotificationPlatform.041.etl, 
UniversalNotificationPlatform.042.etl, UniversalNotificationPlatform.043.etl, Un
iversalNotificationPlatform.044.etl, UniversalNotificationPlatform.045.etl, Univ
ersalNotificationPlatform.046.etl, UniversalNotificationPlatform.047.etl, Univer
salNotificationPlatform.048.etl, UniversalNotificationPlatform.049.etl, Universa
lNotificationPlatform.050.etl]
[C:\Program Files\UNP\Logs\UniversalNotificationPlatform.001.etl, C:\Program Fil
es\UNP\Logs\UniversalNotificationPlatform.002.etl, C:\Program Files\UNP\Logs\Uni
versalNotificationPlatform.003.etl, C:\Program Files\UNP\Logs\UniversalNotificat
ionPlatform.004.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.005
.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.006.etl, C:\Progra
m Files\UNP\Logs\UniversalNotificationPlatform.007.etl, C:\Program Files\UNP\Log
s\UniversalNotificationPlatform.008.etl, C:\Program Files\UNP\Logs\UniversalNoti
ficationPlatform.009.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatfor
m.010.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.011.etl, C:\P
rogram Files\UNP\Logs\UniversalNotificationPlatform.012.etl, C:\Program Files\UN
P\Logs\UniversalNotificationPlatform.013.etl, C:\Program Files\UNP\Logs\Universa
lNotificationPlatform.014.etl, C:\Program Files\UNP\Logs\UniversalNotificationPl
atform.015.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.016.etl,
 C:\Program Files\UNP\Logs\UniversalNotificationPlatform.017.etl, C:\Program Fil
es\UNP\Logs\UniversalNotificationPlatform.018.etl, C:\Program Files\UNP\Logs\Uni
versalNotificationPlatform.019.etl, C:\Program Files\UNP\Logs\UniversalNotificat
ionPlatform.020.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.021
.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.022.etl, C:\Progra
m Files\UNP\Logs\UniversalNotificationPlatform.023.etl, C:\Program Files\UNP\Log
s\UniversalNotificationPlatform.024.etl, C:\Program Files\UNP\Logs\UniversalNoti
ficationPlatform.025.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatfor
m.026.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.027.etl, C:\P
rogram Files\UNP\Logs\UniversalNotificationPlatform.028.etl, C:\Program Files\UN
P\Logs\UniversalNotificationPlatform.029.etl, C:\Program Files\UNP\Logs\Universa
lNotificationPlatform.030.etl, C:\Program Files\UNP\Logs\UniversalNotificationPl
atform.031.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.032.etl,
 C:\Program Files\UNP\Logs\UniversalNotificationPlatform.033.etl, C:\Program Fil
es\UNP\Logs\UniversalNotificationPlatform.034.etl, C:\Program Files\UNP\Logs\Uni
versalNotificationPlatform.035.etl, C:\Program Files\UNP\Logs\UniversalNotificat
ionPlatform.036.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.037
.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.038.etl, C:\Progra
m Files\UNP\Logs\UniversalNotificationPlatform.039.etl, C:\Program Files\UNP\Log
s\UniversalNotificationPlatform.040.etl, C:\Program Files\UNP\Logs\UniversalNoti
ficationPlatform.041.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatfor
m.042.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.043.etl, C:\P
rogram Files\UNP\Logs\UniversalNotificationPlatform.044.etl, C:\Program Files\UN
P\Logs\UniversalNotificationPlatform.045.etl, C:\Program Files\UNP\Logs\Universa
lNotificationPlatform.046.etl, C:\Program Files\UNP\Logs\UniversalNotificationPl
atform.047.etl, C:\Program Files\UNP\Logs\UniversalNotificationPlatform.048.etl,
 C:\Program Files\UNP\Logs\UniversalNotificationPlatform.049.etl, C:\Program Fil
es\UNP\Logs\UniversalNotificationPlatform.050.etl]
UniversalNotificationPlatform.001.etl
UniversalNotificationPlatform.002.etl
UniversalNotificationPlatform.003.etl
UniversalNotificationPlatform.004.etl
UniversalNotificationPlatform.005.etl
UniversalNotificationPlatform.006.etl
UniversalNotificationPlatform.007.etl
UniversalNotificationPlatform.008.etl
UniversalNotificationPlatform.009.etl
UniversalNotificationPlatform.010.etl
UniversalNotificationPlatform.011.etl
UniversalNotificationPlatform.012.etl
UniversalNotificationPlatform.013.etl
UniversalNotificationPlatform.014.etl
UniversalNotificationPlatform.015.etl
UniversalNotificationPlatform.016.etl
UniversalNotificationPlatform.017.etl
UniversalNotificationPlatform.018.etl
UniversalNotificationPlatform.019.etl
UniversalNotificationPlatform.020.etl
UniversalNotificationPlatform.021.etl
UniversalNotificationPlatform.022.etl
UniversalNotificationPlatform.023.etl
UniversalNotificationPlatform.024.etl
UniversalNotificationPlatform.025.etl
UniversalNotificationPlatform.026.etl
UniversalNotificationPlatform.027.etl
UniversalNotificationPlatform.028.etl
UniversalNotificationPlatform.029.etl
UniversalNotificationPlatform.030.etl
UniversalNotificationPlatform.031.etl
UniversalNotificationPlatform.032.etl
UniversalNotificationPlatform.033.etl
UniversalNotificationPlatform.034.etl
UniversalNotificationPlatform.035.etl
UniversalNotificationPlatform.036.etl
UniversalNotificationPlatform.037.etl
UniversalNotificationPlatform.038.etl
UniversalNotificationPlatform.039.etl
UniversalNotificationPlatform.040.etl
UniversalNotificationPlatform.041.etl
UniversalNotificationPlatform.042.etl
UniversalNotificationPlatform.043.etl
UniversalNotificationPlatform.044.etl
UniversalNotificationPlatform.045.etl
UniversalNotificationPlatform.046.etl
UniversalNotificationPlatform.047.etl
UniversalNotificationPlatform.048.etl
UniversalNotificationPlatform.049.etl
UniversalNotificationPlatform.050.etl
====================
UniversalNotificationPlatform.001.etl-131072
UniversalNotificationPlatform.002.etl-131072
UniversalNotificationPlatform.003.etl-131072
UniversalNotificationPlatform.004.etl-131072
UniversalNotificationPlatform.005.etl-131072
UniversalNotificationPlatform.006.etl-131072
UniversalNotificationPlatform.007.etl-131072
UniversalNotificationPlatform.008.etl-131072
UniversalNotificationPlatform.009.etl-131072
UniversalNotificationPlatform.010.etl-131072
UniversalNotificationPlatform.011.etl-131072
UniversalNotificationPlatform.012.etl-131072
UniversalNotificationPlatform.013.etl-131072
UniversalNotificationPlatform.014.etl-131072
UniversalNotificationPlatform.015.etl-131072
UniversalNotificationPlatform.016.etl-131072
UniversalNotificationPlatform.017.etl-131072
UniversalNotificationPlatform.018.etl-131072
UniversalNotificationPlatform.019.etl-131072
UniversalNotificationPlatform.020.etl-131072
UniversalNotificationPlatform.021.etl-131072
UniversalNotificationPlatform.022.etl-131072
UniversalNotificationPlatform.023.etl-131072
UniversalNotificationPlatform.024.etl-131072
UniversalNotificationPlatform.025.etl-131072
UniversalNotificationPlatform.026.etl-131072
UniversalNotificationPlatform.027.etl-131072
UniversalNotificationPlatform.028.etl-131072
UniversalNotificationPlatform.029.etl-131072
UniversalNotificationPlatform.030.etl-131072
UniversalNotificationPlatform.031.etl-131072
UniversalNotificationPlatform.032.etl-131072
UniversalNotificationPlatform.033.etl-131072
UniversalNotificationPlatform.034.etl-131072
UniversalNotificationPlatform.035.etl-131072
UniversalNotificationPlatform.036.etl-131072
UniversalNotificationPlatform.037.etl-131072
UniversalNotificationPlatform.038.etl-131072
UniversalNotificationPlatform.039.etl-131072
UniversalNotificationPlatform.040.etl-131072
UniversalNotificationPlatform.041.etl-131072
UniversalNotificationPlatform.042.etl-131072
UniversalNotificationPlatform.043.etl-131072
UniversalNotificationPlatform.044.etl-131072
UniversalNotificationPlatform.045.etl-131072
UniversalNotificationPlatform.046.etl-131072
UniversalNotificationPlatform.047.etl-131072
UniversalNotificationPlatform.048.etl-131072
UniversalNotificationPlatform.049.etl-131072
UniversalNotificationPlatform.050.etl-131072

`

你可能感兴趣的:(JAVA——IO)