1 运用File类进行文件操作
2 file.getPath() 相对路径
3 file.getAbsolutePath() 绝对路径
4 路径:/ \\
5 方法:
6 创建文件
7 exists()
8 createNewFile()
9
10 删除文件
11 exists()
12 delete()
13
14 获取文件相关信息
15 isFile()
16 isDirectory()
17 getName()
18 getPath()
19 getAbsolutePath()
20 length()
21 范例1:
22 public class FileMethods {
23 //创建文件
24 public void creatFile(File file){
25 if (!file.exists()) {
26 try {
27 file.createNewFile();
28 System.out.println("文件创建成功");
29 } catch (IOException e) {
30 e.printStackTrace();
31 }
32 }
33 }
34
35 //查看文件信息
36 public void showFileInfo(File file){
37 if (file.exists()) {
38 if (file.isFile()) {
39 System.out.println("文件名:" + file.getName());
40 System.out.println("文件绝对路径:" + file.getAbsolutePath());
41 System.out.println("文件相对路径:" + file.getPath());
42 System.out.println("文件大小(字节):" + file.length());
43 }
44
45 if (file.isDirectory()) {
46 System.out.println("这是一个目录");
47 }
48 }
49 }
50
51 //删除文件
52 public void deleteFile(File file){
53 if (file.exists()) {
54 file.delete();
55 System.out.println("文件删除成功");
56 }
57 }
58
59 //测试
60 public static void main(String[] args) {
61 //调用方法,创建方法的对象
62 FileMethods fm = new FileMethods();
63 //创建文件对象
64 // File file = new File("e:" + File.separator + "Java实训课总结" + File.separator + "2016.3.26" + File.separator + "FileDemo.txt");
65 File file = new File("FileDemo.txt");
66 fm.creatFile(file);
67 fm.showFileInfo(file);
68 fm.deleteFile(file);
69 }
70 }
71
72 范例2:
73 public class FileMethod {
74 //判断文件夹是否存在
75 public String existFile(String pathName){
76 File file = new File(pathName);
77 if (!file.exists()) {
78 return null;
79 } else {
80 return pathName;
81 }
82 }
83
84 //显示文件信息
85 public void showFileInfo(String pathName){
86 File file = new File(pathName);
87 String str = existFile(pathName);
88 if (str != null) {
89 file.getAbsolutePath();
90 System.out.println("文件名:" + file.getName());
91 System.out.println("文件大小:" + file.length());
92 System.out.println("文件绝对路径:" + file.getAbsolutePath());
93 System.out.println("文件相对路径:" + file.getPath());
94 // System.out.println("文件大小(单位:字节):" + new BigDecimal((double)file.length()/1024/1024).divide(new BigDecimal(1), 2, BigDecimal.ROUND_HALF_UP) + "M");
95 }
96 }
97
98 //分割类
99 public long unitSize = 0;//每个小文件的大小
100 public String targetDir = null;//分割后小文件所在的文件夹
101
102 public int cutFile(String pathName) throws IOException{
103 File file = new File(pathName);
104 String str = existFile(pathName);
105 long size = file.length();//总字节数
106 int count = 0;//小文件数
107 long pos = 0;//当前位置
108 long last = 0;//剩余字节数
109 DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(pathName),(int)unitSize));
110 byte[] data = new byte[(int)this.unitSize];
111 while (pos < size) {
112 count++;
113 last = size - pos;
114 if (last < this.unitSize) {
115 data = new byte[(int)last];
116 }
117 dis.read(data);
118 pos = pos + data.length;
119 //写小文件
120 try {
121 RandomAccessFile raFile = new RandomAccessFile(this.targetDir + "-" + count,"rw");
122 System.out.println(this.targetDir + "-" + count);
123 raFile.write(data);
124 raFile.close();
125 } catch (FileNotFoundException e) {
126 e.printStackTrace();
127 } catch (IOException e) {
128 e.printStackTrace();
129 }
130 }
131 //记载小文件数
132 File file2 = new File(this.targetDir + "_count");
133 BufferedWriter bw = new BufferedWriter(new FileWriter(file2));
134 String str2 = (new Integer(count)).toString();
135 bw.write(str2, 0, str2.length());
136 bw.flush();
137 bw.close();
138 return count;
139 }
140
141 public long getUnitSize() {
142 return unitSize;
143 }
144
145 public void setUnitSize(long unitSize) {
146 this.unitSize = unitSize;
147 }
148
149 public String getTargetDir() {
150 return targetDir;
151 }
152
153 public void setTargetDir(String targetDir) {
154 this.targetDir = targetDir;
155 }
156 }
157
158 //测试
159 public class SegmentationTxt {
160 public static void main(String[] args) {
161 Scanner input = new Scanner(System.in);
162 FileMethod fm = new FileMethod();
163 //1.输入文件路径
164 System.out.println("请输入文件路径:");
165 String pathName = input.next();
166 File file = new File(pathName);
167 String str = fm.existFile(pathName);
168 while (str == null) {
169 System.out.println("文件不存在!");
170 System.out.println("请输入文件路径:");
171 pathName = input.next();
172 str = fm.existFile(pathName);
173 }
174 System.out.println("======文件信息=====");
175 //2.打印文件信息
176 fm.showFileInfo(pathName);
177 //3.分割文件
178 System.out.println("请输入文件分割的大小(单位byte):");
179 int dividNum = input.nextInt();
180 System.out.println("开始分割......");
181 try {
182 Thread.sleep(2000);
183 fm.setUnitSize(dividNum);
184 fm.setTargetDir(pathName);
185 fm.cutFile(pathName);
186 } catch (IOException e) {
187 e.printStackTrace();
188 } catch (InterruptedException e) {
189 e.printStackTrace();
190 }
191 }
192 }