0927-----homework

 1 /**
 2   *homework0926
 3   *@author:kai li
 4   */
 5 package com.kai.li.homework0927;
 6 import java.util.List;
 7 import java.util.ArrayList;
 8 import java.nio.file.Files;
 9 import java.nio.file.Paths;
10 import java.nio.file.StandardOpenOption;
11 import java.nio.charset.Charset;
12 import java.util.Scanner;
13 import java.io.BufferedReader;
14 import java.io.FileReader;
15 import java.io.File;
16 /**
17   *following class is client 
18   */
19 public class HomeWork0927{
20     public static void main(String[] args)throws Exception{
21         
22         /**
23           *question one
24           */
25 
26         /*create data source*/
27 
28     String fileTarget="d:/text1.txt";
29     List<String> lines=new ArrayList<>();
30     lines.add("学号:17,姓名:小八。");
31     lines.add("学号:18,姓名:小七。");
32     
33     /*write and read to a file*/
34     
35     //write 
36     Files.write(Paths.get(fileTarget),lines,Charset.defaultCharset(),StandardOpenOption.CREATE);
37     System.out.println("write is success");
38     //read
39     byte[] contentBytes=Files.readAllBytes(Paths.get(fileTarget));
40     System.out.println(new String(contentBytes));
41 
42     /**
43       *question two
44       */
45     
46     //read
47     byte[] contentBytes2=Files.readAllBytes(Paths.get(fileTarget));
48     //input scanner
49     System.out.println("please input file name");
50     Scanner scanner=new Scanner(System.in);
51     String txtName=scanner.nextLine();
52     //write
53     String fileTarget2="d:/"+txtName;
54     Files.write(Paths.get(fileTarget2),contentBytes2,StandardOpenOption.CREATE);
55     System.out.println("write is success");
56 
57     /**
58       *question three
59       */
60     
61     BufferedReader br=new BufferedReader(new FileReader(fileTarget2));
62     String contentFileName=br.readLine();
63     File file=new File(fileTarget2);
64     File target=new File("e:/"+contentFileName+".txt");
65     file.renameTo(target);
66     System.out.println("move success");
67     }
68 }

 

转载于:https://www.cnblogs.com/kaililikai/p/5917909.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值