1、首先了解文件流的相关概念:
2、文件file类的基本用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
public class demo_1 {
public static void main(string[] args) {
//创建一个文件对象
file f= new file( "e:\\aa.txt" );
//得到文件的路径
system.out.println( "文件路径:" +f.getabsolutepath());
//得到文件的大小,字节数
system.out.println( "文件大小:" +f.length());
system.out.println( "文件可读:" +f.canread());
//创建文件夹
file f3= new file( "e:\\ff" );
if (!f3.isdirectory()){
//创建
f3.mkdir();
} else {
system.out.println( "文件夹已存在" );
}
//创建文件
file f2= new file( "e:\\ff\\hsp.txt" );
if (!f2.exists()){
//可以创建
try {
f2.createnewfile();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
} else {
system.out.print( "有文件,不能创建" );
}
//列出一个文件夹下面的所有文件
file f4= new file( "e://安装包下载" ); //引号下也可写成e:/ff
if (f4.isdirectory()){
file lists[]=f4.listfiles();
for ( int i= 0 ;i<lists.length;i++){
system.out.println( "文件名:" +lists[i].getname());
}
}
}
}
|
以上这篇java文件(io)编程_基于file类的基本用法(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/cxq1126/archive/2017/08/10/7340423.html