test01.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. package com.sf.iotest;
  2. import org.junit.Test;
  3. import java.io.*;
  4. import java.util.*;
  5. public class test01 {
  6. public static void main(String[] args) {
  7. String pathname = "c:\\aaa.txt";
  8. File file = new File(pathname);
  9. // 文件路径名
  10. String pathname2 = "D:\\aaa\\bbb.txt";
  11. File file2 = new File(pathname2);
  12. // 通过父路径和子路径字符串
  13. String parent = "d:\\aaa";
  14. String child = "bbb.txt";
  15. File file3 = new File(parent, child);
  16. // 通过父级File对象和子路径字符串
  17. File parentDir = new File("d:\\aaa");
  18. String childFile = "bbb.txt";
  19. File file4 = new File(parentDir, childFile);
  20. }
  21. @Test
  22. public void t2(){
  23. File f1 = new File("d:\\a.txt");
  24. System.out.println("文件/目录的名称:" + f1.getName());
  25. System.out.println("文件/目录的构造路径名:" + f1.getPath());
  26. System.out.println("文件/目录的绝对路径名:" + f1.getAbsolutePath());
  27. System.out.println("文件/目录的父目录名:" + f1.getParent());
  28. }
  29. @Test
  30. public void t3(){
  31. File f3 = new File("a.txt");
  32. System.out.println("user.dir =" + System.getProperty("user.dir"));
  33. System.out.println("文件/目录的名称:" + f3.getName());
  34. System.out.println("文件/目录的构造路径名:" + f3.getPath());
  35. System.out.println("文件/目录的绝对路径名:" + f3.getAbsolutePath());
  36. System.out.println("文件/目录的父目录名:" + f3.getParent());
  37. }
  38. @Test
  39. public void t4(){
  40. File f = new File("d:/aaa/bbb.txt");
  41. System.out.println("文件构造路径:"+f.getPath());
  42. System.out.println("文件名称:"+f.getName());
  43. System.out.println("文件长度:"+f.length()+"字节");
  44. System.out.println("文件最后修改时间:" + f.lastModified());
  45. File f2 = new File("d:/aaa");
  46. System.out.println("目录构造路径:"+f2.getPath());
  47. System.out.println("目录名称:"+f2.getName());
  48. System.out.println("目录长度:"+f2.length()+"字节");
  49. System.out.println("文件最后修改时间:" + new Date(f.lastModified()));
  50. }
  51. @Test
  52. public void t5(){
  53. File dir = new File("d:/ruanjian");
  54. String[] subs = dir.list();
  55. System.out.println(Arrays.toString(subs));
  56. for (String sub : subs) {
  57. System.out.println(sub);
  58. }
  59. }
  60. @Test
  61. public void t6(){
  62. File file1 = new File("d:/a.txt");
  63. File file2 = new File("d:/b.txt");
  64. System.out.println(file1.getName());
  65. boolean b = file1.renameTo(file2);
  66. System.out.println(b);
  67. }
  68. @Test
  69. public void t7(){
  70. File f = new File("d:\\a.txt");
  71. File f2 = new File("d:\\tmp");
  72. // 判断是否存在
  73. System.out.println("d:\\aaa\\bbb.java 是否存在:"+f.exists());
  74. System.out.println("d:\\aaa 是否存在:"+f2.exists());
  75. // 判断是文件还是目录
  76. System.out.println("d:\\aaa 文件?:"+f2.isFile());
  77. System.out.println("d:\\aaa 目录?:"+f2.isDirectory());
  78. }
  79. @Test
  80. public void t8() throws IOException {
  81. // 文件的创建
  82. File f = new File("aaa.txt");
  83. System.out.println("aaa.txt是否存在:"+f.exists());
  84. System.out.println("aaa.txt是否创建:"+f.createNewFile());
  85. System.out.println("aaa.txt是否存在:"+f.exists());
  86. // 目录的创建
  87. File f2= new File("newDir");
  88. System.out.println("newDir是否存在:"+f2.exists());
  89. System.out.println("newDir是否创建:"+f2.mkdir());
  90. System.out.println("newDir是否存在:"+f2.exists());
  91. // 创建一级目录
  92. File f3= new File("newDira\\newDirb");
  93. System.out.println("newDira\\newDirb创建:" + f3.mkdir());
  94. File f4= new File("newDir\\newDirb");
  95. System.out.println("newDir\\newDirb创建:" + f4.mkdir());
  96. // 创建多级目录
  97. File f5= new File("newDira\\newDirb");
  98. System.out.println("newDira\\newDirb创建:" + f5.mkdirs());
  99. // 文件的删除
  100. System.out.println("aaa.txt删除:" + f.delete());
  101. // 目录的删除
  102. System.out.println("newDir删除:" + f2.delete());
  103. System.out.println("newDir\\newDirb删除:" + f4.delete());
  104. }
  105. @Test
  106. public void t9() throws IOException {
  107. //利用File构造器,new一个文件目录file
  108. // 在其中创建 当前日期目录 之后创建helloworld.java文件
  109. //编写方法,实现删除file中指定文件的操作
  110. File file = new File("D:\\file");
  111. boolean mkdirs = file.mkdirs();
  112. if (mkdirs) {
  113. File file1 = new File("D:\\file\\a.java");
  114. file1.createNewFile();
  115. }
  116. }
  117. /**
  118. * 判断指定目录下是否有后缀名为.jpg的文件。如果有,就输出该文件名称
  119. */
  120. @Test
  121. public void t10(){
  122. File srcFile = new File("d:\\demo");
  123. String[] fileNames = srcFile.list();
  124. for(String fileName : fileNames){
  125. if(fileName.endsWith(".jpg")){
  126. System.out.println(fileName);
  127. }
  128. }
  129. }
  130. /**
  131. * 方法二
  132. * 判断指定目录下是否有后缀名为.jpg的文件。如果有,就输出该文件名称
  133. */
  134. @Test
  135. public void t11(){
  136. File srcFile = new File("d:\\demo");
  137. File[] listFiles = srcFile.listFiles();
  138. for(File file : listFiles){
  139. if(file.getName().endsWith(".jpg")){
  140. System.out.println(file.getAbsolutePath());
  141. }
  142. }
  143. }
  144. /**
  145. *判断指定目录下是否有后缀名为.jpg的文件。如果有,就输出该文件名称
  146. * * File类提供了两个文件过滤器方法
  147. * * public String[] list(FilenameFilter filter)
  148. * * public File[] listFiles(FileFilter filter)
  149. */
  150. @Test
  151. public void t12(){
  152. File file = new File("d:\\demo");
  153. File[] files = file.listFiles(new FilenameFilter() {
  154. @Override
  155. public boolean accept(File dir, String name) {
  156. return name.endsWith(".jsp");
  157. }
  158. });
  159. for (File file1 : files) {
  160. System.out.println(file1.getAbsolutePath());
  161. }
  162. }
  163. /**
  164. * 练习3:遍历指定目录所有文件名称,包括子文件目录中的文件。
  165. * 拓展1:并计算指定目录占用空间的大小
  166. * 拓展2:删除指定文件目录及其下的所有文件
  167. */
  168. @Test
  169. public void t13(){
  170. /**
  171. * 代码在另外一个类(ListFilesTest)中的主函数中
  172. */
  173. }
  174. /**
  175. * 读取hello.txt文件中的字符数据,并显示在控制台上
  176. */
  177. @Test
  178. public void t14() throws IOException {
  179. // 1、创建file类的对象 对应着物理磁盘上的某个文件
  180. File file = new File("d:\\a.txt");
  181. //2 、创建fileReader对象 将file类的对象作为参数传递到fileRead的构造器中
  182. FileReader fr = new FileReader(file);
  183. //3、通过相关流的方法 读取文件中的数据
  184. int data;
  185. while ((data = fr.read()) !=-1){
  186. System.out.println((char) data);
  187. }
  188. //4、关闭相关的流资源 避免出现内存泄露
  189. fr.close();
  190. }
  191. /**
  192. * 实现方法3 调用read(char[] cbuf),每次从文件中读取多个字符 FileReader --read()
  193. * @throws IOException
  194. */
  195. @Test
  196. public void t15() throws IOException {
  197. FileReader fr = null;
  198. try {
  199. //1. 创建File类的对象,对应着物理磁盘上的某个文件
  200. File file = new File("d:\\a.txt");
  201. //2. 创建FileReader流对象,将File类的对象作为参数传递到FileReader的构造器中
  202. fr = new FileReader(file);
  203. //3. 通过相关流的方法,读取文件中的数据
  204. char[] cbuf = new char[5];
  205. /*
  206. * read(char[] cbuf) : 每次将文件中的数据读入到cbuf数组中,并返回读入到数组中的
  207. * 字符的个数。
  208. * */
  209. int len; //记录每次读入的字符的个数
  210. while ((len = fr.read(cbuf)) != -1) {
  211. //处理char[]数组即可
  212. //正确:
  213. String str = new String(cbuf, 0, len);
  214. System.out.print(str);
  215. }
  216. } catch (IOException e) {
  217. e.printStackTrace();
  218. } finally {
  219. //4. 关闭相关的流资源,避免出现内存泄漏
  220. if (fr != null)
  221. fr.close();
  222. }
  223. }
  224. @Test
  225. public void t16() throws IOException {
  226. // 使用文件名称创建流对象
  227. FileWriter fw = new FileWriter(new File("d:\\fw.txt"));
  228. // 写出数据
  229. fw.write(97); // 写出第1个字符
  230. fw.write('b'); // 写出第2个字符
  231. fw.write('C'); // 写出第3个字符
  232. fw.write(30000); // 写出第4个字符,中文编码表中30000对应一个汉字。
  233. //关闭资源
  234. fw.close();
  235. }
  236. @Test
  237. public void t17() throws IOException {
  238. // 使用文件名称创建流对象
  239. FileWriter fw = new FileWriter(new File("fw.txt"));
  240. // 字符串转换为字节数组
  241. char[] chars = "爱扣钉".toCharArray();
  242. // 写出字符数组
  243. fw.write(chars); // 爱扣钉
  244. // 写出从索引1开始,2个字符。
  245. fw.write(chars,1,2); // 钉钉
  246. // 关闭资源
  247. fw.close();
  248. }
  249. @Test
  250. public void t18() throws IOException {
  251. FileWriter fw = null;
  252. try {
  253. //1. 创建File的对象
  254. File file = new File("personinfo.txt");
  255. //2. 创建FileWriter的对象,将File对象作为参数传递到FileWriter的构造器中
  256. //如果输出的文件已存在,则会对现有的文件进行覆盖
  257. fw = new FileWriter(file);
  258. // fw = new FileWriter(file,false);
  259. //如果输出的文件已存在,则会在现有的文件末尾写入数据
  260. // fw = new FileWriter(file,true);
  261. //3. 调用相关的方法,实现数据的写出操作
  262. //write(String str) / write(char[] cbuf)
  263. fw.write("hello world");
  264. fw.write("world".toCharArray());
  265. } catch (IOException e) {
  266. e.printStackTrace();
  267. } finally {
  268. //4. 关闭资源,避免内存泄漏
  269. try {
  270. if (fw != null)
  271. fw.close();
  272. } catch (IOException e) {
  273. throw new RuntimeException(e);
  274. }
  275. }
  276. }
  277. @Test
  278. public void t19() throws IOException {
  279. //复制文件 b.txt 到 upload下
  280. FileReader fr = new FileReader("d:\\a.txt");
  281. FileWriter fw = new FileWriter("d:\\b.txt",true);
  282. //数组
  283. char[] chars = new char[1024*8];
  284. int len;
  285. while ( (len=fr.read(chars)) != -1 ){
  286. fw.write(chars,0,len);
  287. }
  288. //关闭
  289. fw.close();
  290. fr.close();
  291. }
  292. @Test
  293. public void t20() throws IOException {
  294. // 使用文件名称创建流对象
  295. FileWriter fw = new FileWriter("fw.txt");
  296. // 写出数据,通过flush
  297. fw.write('刷'); // 写出第1个字符
  298. fw.flush();
  299. fw.write('新'); // 继续写出第2个字符,写出成功
  300. fw.flush();
  301. // 写出数据,通过close
  302. fw.write('关'); // 写出第1个字符
  303. fw.close();
  304. fw.write('闭'); // 继续写出第2个字符,【报错】java.io.IOException: Stream closed
  305. fw.close();
  306. }
  307. @Test
  308. public void t21() throws IOException {
  309. // 使用文件名称创建流对象
  310. FileInputStream fis = new FileInputStream("D:\\a.txt");
  311. // 读取数据,返回一个字节
  312. int read = fis.read();
  313. System.out.println((char) read);
  314. read = fis.read();
  315. System.out.println((char) read);
  316. read = fis.read();
  317. System.out.println((char) read);
  318. read = fis.read();
  319. System.out.println((char) read);
  320. read = fis.read();
  321. System.out.println((char) read);
  322. read = fis.read();
  323. System.out.println((char) read);
  324. read = fis.read();
  325. System.out.println((char) read);
  326. read = fis.read();
  327. System.out.println((char) read);
  328. read = fis.read();
  329. System.out.println((char) read);
  330. read = fis.read();
  331. System.out.println((char) read);
  332. // 读取到末尾,返回-1
  333. read = fis.read();
  334. System.out.println(read);
  335. // 关闭资源
  336. fis.close();
  337. }
  338. @Test
  339. public void t22() throws IOException {
  340. // 使用文件名称创建流对象
  341. FileInputStream fis = new FileInputStream("D:\\a.txt");
  342. // 定义变量,保存数据
  343. int b;
  344. // 循环读取
  345. while ((b = fis.read())!=-1) {
  346. System.out.println((char)b);
  347. }
  348. // 关闭资源
  349. fis.close();
  350. }
  351. @Test
  352. public void t23()throws IOException{
  353. // 使用文件名称创建流对象.
  354. FileInputStream fis = new FileInputStream("D:\\a.txt"); // 文件中为abcde
  355. // 定义变量,作为有效个数
  356. int len;
  357. // 定义字节数组,作为装字节数据的容器
  358. byte[] b = new byte[2];
  359. // 循环读取
  360. while (( len= fis.read(b))!=-1) {
  361. // 每次读取后,把数组变成字符串打印
  362. System.out.println(new String(b));
  363. }
  364. // 关闭资源
  365. fis.close();
  366. /*
  367. 输出结果:
  368. ab
  369. cd
  370. ed
  371. 最后错误数据`d`,是由于最后一次读取时,只读取一个字节`e`,数组中,
  372. 上次读取的数据没有被完全替换,所以要通过`len` ,获取有效的字节
  373. */
  374. }
  375. @Test
  376. public void t24()throws IOException{
  377. // 使用文件名称创建流对象.
  378. FileInputStream fis = new FileInputStream("D:\\a.txt"); // 文件中为abcde
  379. // 定义变量,作为有效个数
  380. int len;
  381. // 定义字节数组,作为装字节数据的容器
  382. byte[] b = new byte[2];
  383. // 循环读取
  384. while (( len= fis.read(b))!=-1) {
  385. // 每次读取后,把数组的有效字节部分,变成字符串打印
  386. System.out.println(new String(b,0,len));// len 每次读取的有效字节个数
  387. }
  388. // 关闭资源
  389. fis.close();
  390. /*
  391. 输出结果:
  392. ab
  393. cd
  394. e
  395. */
  396. }
  397. @Test
  398. public void t25() throws IOException {
  399. // 使用文件名称创建流对象
  400. FileOutputStream fos = new FileOutputStream("fos.txt");
  401. // 写出数据
  402. fos.write(97); // 写出第1个字节
  403. fos.write(98); // 写出第2个字节
  404. fos.write(99); // 写出第3个字节
  405. // 关闭资源
  406. fos.close();
  407. /* 输出结果:abc*/
  408. }
  409. @Test
  410. public void t26() throws IOException {
  411. // 使用文件名称创建流对象
  412. FileOutputStream fos = new FileOutputStream("fos.txt");
  413. // 字符串转换为字节数组
  414. byte[] b = "abcde".getBytes();
  415. // 写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。
  416. fos.write(b,2,2);
  417. // 关闭资源
  418. fos.close();
  419. }
  420. @Test
  421. public void t27() throws IOException{
  422. // 使用文件名称创建流对象
  423. FileOutputStream fos = new FileOutputStream("fos.txt",true);
  424. // 字符串转换为字节数组
  425. byte[] b = "abcde".getBytes();
  426. fos.write(b);
  427. // 关闭资源
  428. fos.close();
  429. }
  430. //使用FileInputStream\FileOutputStream,实现对文件的复制
  431. @Test
  432. public void test28() {
  433. FileInputStream fis = null;
  434. FileOutputStream fos = null;
  435. try {
  436. //1. 造文件-造流
  437. //复制图片:成功
  438. // fis = new FileInputStream(new File("pony.jpg"));
  439. // fos = new FileOutputStream(new File("pony_copy1.jpg"));
  440. //复制文本文件:成功
  441. fis = new FileInputStream(new File("D:\\a.txt"));
  442. fos = new FileOutputStream(new File("D:\\hello1.txt"));
  443. //2. 复制操作(读、写)
  444. byte[] buffer = new byte[1024];
  445. int len;//每次读入到buffer中字节的个数
  446. while ((len = fis.read(buffer)) != -1) {
  447. fos.write(buffer, 0, len);
  448. }
  449. System.out.println("复制成功");
  450. } catch (IOException e) {
  451. throw new RuntimeException(e);
  452. } finally {
  453. //3. 关闭资源
  454. try {
  455. if (fos != null)
  456. fos.close();
  457. } catch (IOException e) {
  458. throw new RuntimeException(e);
  459. }
  460. try {
  461. if (fis != null)
  462. fis.close();
  463. } catch (IOException e) {
  464. throw new RuntimeException(e);
  465. }
  466. }
  467. }
  468. //图片的复制
  469. @Test
  470. public void t29() throws Exception {
  471. FileInputStream fis = new FileInputStream("C:\\Users\\Lenovo\\Desktop\\123.jpg");
  472. FileOutputStream fos = new FileOutputStream("C:\\Users\\Lenovo\\Desktop\\3.jpg");
  473. //数组 缓冲
  474. byte[] bytes = new byte[8192];
  475. int len;
  476. //循环读取
  477. while ( ( len = fis.read(bytes) ) != -1 ){
  478. //输出
  479. fos.write(bytes,0,len);
  480. }
  481. //关闭
  482. fos.close();
  483. fis.close();
  484. }
  485. @Test
  486. public void t30() {
  487. /*
  488. * 图片的加密
  489. * */
  490. FileInputStream fis = null;
  491. FileOutputStream fos = null;
  492. try {
  493. File file1 = new File("C:\\Users\\Lenovo\\Desktop\\123.jpg");
  494. File file2 = new File("C:\\Users\\Lenovo\\Desktop\\3.jpg");
  495. fis = new FileInputStream(file1);
  496. fos = new FileOutputStream(file2);
  497. //方式1:每次读入一个字节,效率低
  498. // int data;
  499. // while((data = fis.read()) != -1){
  500. // fos.write(data ^ 5);
  501. // }
  502. //方式2:每次读入一个字节数组,效率高
  503. int len;
  504. byte[] buffer = new byte[1024];
  505. while ((len = fis.read(buffer)) != -1) {
  506. for (int i = 0; i < len; i++) {
  507. buffer[i] = (byte) (buffer[i] ^ 5);
  508. }
  509. fos.write(buffer, 0, len);
  510. }
  511. System.out.println("加密成功");
  512. } catch (IOException e) {
  513. e.printStackTrace();
  514. } finally {
  515. try {
  516. fos.close();
  517. } catch (IOException e) {
  518. e.printStackTrace();
  519. }
  520. try {
  521. fis.close();
  522. } catch (IOException e) {
  523. e.printStackTrace();
  524. }
  525. }
  526. }
  527. /*
  528. * 图片的解密
  529. * */
  530. @Test
  531. public void t31(){
  532. FileInputStream fis = null;
  533. FileOutputStream fos = null;
  534. try {
  535. File file1 = new File("C:\\Users\\Lenovo\\Desktop\\3.jpg");
  536. File file2 = new File("C:\\Users\\Lenovo\\Desktop\\12345.jpg");
  537. fis = new FileInputStream(file1);
  538. fos = new FileOutputStream(file2);
  539. //方式1:每次读入一个字节,效率低
  540. // int data;
  541. // while((data = fis.read()) != -1){
  542. // fos.write(data ^ 5);
  543. // }
  544. //方式2:每次读入一个字节数组,效率高
  545. int len;
  546. byte[] buffer = new byte[1024];
  547. while((len = fis.read(buffer)) != -1){
  548. for(int i = 0;i < len;i++){
  549. buffer[i] = (byte) (buffer[i] ^ 5);
  550. }
  551. fos.write(buffer,0,len);
  552. }
  553. System.out.println("解密成功");
  554. } catch (IOException e) {
  555. e.printStackTrace();
  556. } finally {
  557. try {
  558. fos.close();
  559. } catch (IOException e) {
  560. e.printStackTrace();
  561. }
  562. try {
  563. fis.close();
  564. } catch (IOException e) {
  565. e.printStackTrace();
  566. }
  567. }
  568. }
  569. /**
  570. * 缓存流
  571. */
  572. public void copyFileWithFileStream(String srcPath,String destPath) throws IOException{
  573. FileInputStream fis = null;
  574. FileOutputStream fos = null;
  575. try {
  576. //1. 造文件-造流
  577. fis = new FileInputStream(new File(srcPath));
  578. fos = new FileOutputStream(new File(destPath));
  579. //2. 复制操作(读、写)
  580. byte[] buffer = new byte[100];
  581. int len;//每次读入到buffer中字节的个数
  582. while ((len = fis.read(buffer)) != -1) {
  583. fos.write(buffer, 0, len);
  584. }
  585. System.out.println("复制成功");
  586. } catch (IOException e) {
  587. throw new RuntimeException(e);
  588. } finally {
  589. //3. 关闭资源
  590. fos.close();
  591. fis.close();
  592. }
  593. }
  594. @Test
  595. public void t32() throws IOException{
  596. String srcPath = "D:\\ruanjian\\WeChat\\WeChat.exe";
  597. String destPath = "D:\\ruanjian\\WeChat\\01_WeChat.exe";
  598. long start = System.currentTimeMillis();
  599. copyFileWithFileStream(srcPath,destPath);
  600. long end = System.currentTimeMillis();
  601. System.out.println("花费的时间为:" + (end - start));//46毫秒
  602. }
  603. //方法2:使用BufferedInputStream\BufferedOuputStream实现非文本文件的复制
  604. public void copyFileWithBufferedStream(String srcPath,String destPath){
  605. FileInputStream fis = null;
  606. FileOutputStream fos = null;
  607. BufferedInputStream bis = null;
  608. BufferedOutputStream bos = null;
  609. try {
  610. //1. 造文件
  611. File srcFile = new File(srcPath);
  612. File destFile = new File(destPath);
  613. //2. 造流
  614. fis = new FileInputStream(srcFile);
  615. fos = new FileOutputStream(destFile);
  616. bis = new BufferedInputStream(fis);
  617. bos = new BufferedOutputStream(fos);
  618. //3. 读写操作
  619. int len;
  620. byte[] buffer = new byte[100];
  621. while ((len = bis.read(buffer)) != -1) {
  622. bos.write(buffer, 0, len);
  623. }
  624. System.out.println("复制成功");
  625. } catch (IOException e) {
  626. e.printStackTrace();
  627. } finally {
  628. //4. 关闭资源(如果有多个流,我们需要先关闭外面的流,再关闭内部的流)
  629. try {
  630. if (bos != null)
  631. bos.close();
  632. } catch (IOException e) {
  633. throw new RuntimeException(e);
  634. }
  635. try {
  636. if (bis != null)
  637. bis.close();
  638. } catch (IOException e) {
  639. throw new RuntimeException(e);
  640. }
  641. }
  642. }
  643. @Test
  644. public void t33(){
  645. String srcPath = "D:\\ruanjian\\WeChat\\WeChat.exe";
  646. String destPath = "D:\\ruanjian\\WeChat\\01_WeChat.exe";
  647. long start = System.currentTimeMillis();
  648. copyFileWithBufferedStream(srcPath,destPath);
  649. long end = System.currentTimeMillis();
  650. System.out.println("花费的时间为:" + (end - start));//3毫秒
  651. }
  652. /**
  653. * 字符缓冲流特有方法
  654. */
  655. @Test
  656. public void t34() throws IOException{
  657. // 创建流对象
  658. BufferedReader br = new BufferedReader(new FileReader("out.txt"));
  659. // 定义字符串,保存读取的一行文字
  660. String line;
  661. // 循环读取,读取到最后返回null
  662. while ((line = br.readLine())!=null) {
  663. System.out.println(line);
  664. }
  665. // 释放资源
  666. br.close();
  667. }
  668. @Test
  669. public void testNewLine() throws IOException{
  670. // 创建流对象
  671. BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt"));
  672. // 写出数据
  673. bw.write("爱");
  674. // 写出换行
  675. bw.newLine();
  676. bw.write("扣");
  677. bw.newLine();
  678. bw.write("钉");
  679. bw.newLine();
  680. // 释放资源
  681. bw.close();
  682. }
  683. @Test
  684. public void t35() throws IOException{
  685. //字符
  686. BufferedReader br = new BufferedReader(new FileReader("out.txt"));
  687. BufferedWriter bw = new BufferedWriter(new FileWriter("out1.txt"));
  688. //map集合
  689. HashMap<String,Integer> map = new HashMap<>();
  690. String s;
  691. while ( (s =br.readLine()) != null ){
  692. //获取 s的第一个字符
  693. char name = s.charAt(0);
  694. if (map.containsKey(name)){
  695. Integer integer = map.get(name);
  696. integer++;
  697. //key Steing value Integer
  698. map.put(String.valueOf(name),integer);
  699. }else{
  700. map.put(String.valueOf(name),1);
  701. }
  702. }
  703. //循环map 输出
  704. Set<Map.Entry<String, Integer>> entries = map.entrySet();
  705. //循环输出
  706. for (Map.Entry<String, Integer> entry : entries) {
  707. bw.write(entry.getKey()+":"+entry.getValue());
  708. bw.newLine();
  709. }
  710. bw.close();
  711. br.close();
  712. }
  713. /**
  714. * 使用`FileReader` 读取项目中的文本文件。由于IDEA设置中针对项目设置了UTF-8编码,
  715. * 当读取Windows系统中创建的文本文件时,如果Windows系统默认的是GBK编码,则读入内存中会出现乱码。
  716. */
  717. @Test
  718. public void t36() throws IOException{
  719. FileReader fileReader = new FileReader("D:\\File_GBK.txt");
  720. int data;
  721. while ((data = fileReader.read()) != -1) {
  722. System.out.print((char)data);
  723. }
  724. fileReader.close();
  725. }
  726. /**
  727. * 序列化和反序列化
  728. */
  729. @Test
  730. public void save() throws IOException {
  731. String name = "巫师";
  732. int age = 300;
  733. char gender = '男';
  734. int energy = 5000;
  735. double price = 75.5;
  736. boolean relive = true;
  737. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("game.dat"));
  738. oos.writeUTF(name);
  739. oos.writeInt(age);
  740. oos.writeChar(gender);
  741. oos.writeInt(energy);
  742. oos.writeDouble(price);
  743. oos.writeBoolean(relive);
  744. oos.close();
  745. }
  746. @Test
  747. public void reload()throws IOException{
  748. ObjectInputStream ois = new ObjectInputStream(new FileInputStream("game.dat"));
  749. String name = ois.readUTF();
  750. int age = ois.readInt();
  751. char gender = ois.readChar();
  752. int energy = ois.readInt();
  753. double price = ois.readDouble();
  754. boolean relive = ois.readBoolean();
  755. System.out.println(name+"," + age + "," + gender + "," + energy + "," + price + "," + relive);
  756. ois.close();
  757. }
  758. }