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 }