Commit 3345b763 authored by 时良荣's avatar 时良荣

张国敬 Java培训 = 文件读写

parent 431a2c1a
package com.exercise.fileio;
import java.io.File;
/**
* 张国敬 Java培训 = 文件读写
* 编写一段程序,判断d:/study/io目录是否存在,存在则遍历输出这个目录的所有文件名称,不存在则创建这个目录
* @author elliot
*/
public class ExerciseFirst {
public static void main(String[] args){
String target = "/Users/elliot/JavaProjects/java-exercise/src/main/java/com/exercise/fileio/exercise";
listFiles(target);
}
public static void listFiles(final String path)
{
File file = new File(path);
if(! file.exists()){
System.out.println("该文件不存在,创建该文件");
file.mkdirs();
return;
}
String[] children = file.list();
if(children == null || children.length == 0){
return;
}
for (String child : children){
String childPath = path + File.separator + child;
File childFile = new File(childPath);
if(childFile.isFile()){
System.out.println("找到了一个文件:" + childPath);
}else{
listFiles(childPath);
}
}
}
}
package com.exercise.fileio;
import java.io.*;
/**
* 编写一个文件复制函数,copyFile(String from, String to),from为原文件名,to为复制后的新文件名,复制结束打印
* 所复制的文件大小和用时(毫秒),执行结果如:copy xxx use xxx ms, file size is xxx
* @author elliot
*/
public class ExerciseFourth {
public static void main(String[] args){
String fromFile = "/Users/elliot/JavaProjects/java-exercise/src/main/java/com/exercise/fileio/exercise/test.txt";
String targetFile = "/Users/elliot/JavaProjects/java-exercise/src/main/java/com/exercise/fileio/exercise/test_copy.txt";
copyFile(fromFile, targetFile);
}
/**
* 复制文件
* @param from
* @param to
* @return
*/
public static void copyFile(String from, String to){
long currentTimeMillisStart = System.currentTimeMillis();
File file = new File(from);
if(! file.exists()){
System.out.println("文件不存在");
return;
}
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(file);
out = new FileOutputStream(new File(to));
byte[] bytes = new byte[4096];
int len = 0;
while ((len = in.read(bytes)) > 0){
out.write(bytes, 0, len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long currentTimeMillisEnd = System.currentTimeMillis();
System.out.println("copy from " + from + " use " + (currentTimeMillisEnd - currentTimeMillisStart) + "ms, file size is " + file.length());
}
}
package com.exercise.fileio;
import java.io.File;
/**
* 编写一段程序,删除d:/study/io这个目录下的大于20Kb的文件
* @author elliot
*/
public class ExerciseSecond {
public static void main(String[] args){
String target = "/Users/elliot/JavaProjects/java-exercise/src/main/java/com/exercise/fileio/exercise";
listFilesAndClear(target);
}
public static void listFilesAndClear(final String path)
{
File file = new File(path);
if(! file.exists()){
return;
}
String[] children = file.list();
if(children == null || children.length == 0){
return;
}
for (String child : children){
String childPath = path + File.separator + child;
File childFile = new File(childPath);
if(childFile.isFile() && childFile.length() > (20 * 1024)){
System.out.println("找到了一个大于20KB的文件并删除:" + childPath);
childFile.delete();
}else{
listFilesAndClear(childPath);
}
}
}
}
package com.exercise.fileio;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
/**
* 编写一段程序,循环从键盘输入一行字符,把这行字符首尾颠倒后按行输出到一个文件中,遇到quit后退出
* @author elliot
*/
public class ExerciseThird {
public static void main(String[] args){
System.out.println("请输入一段字符:");
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
System.out.println("输入的字符是:" + s);
// 把这行字符首尾颠倒
String reverseString = stringReverse(s);
System.out.println("倒序后的字符是:" + reverseString);
// 输出到一个文件中
String fileName = "/Users/elliot/JavaProjects/java-exercise/src/main/java/com/exercise/fileio/exercise/test.txt";
stringWrite(reverseString, fileName);
}
/**
* 颠倒字符顺序
* @param string
* @return
*/
public static String stringReverse(String string){
String[] s = string.split("");
String str = new String();
List<String> list = new ArrayList<>(Arrays.asList(s));
Collections.reverse(list);
for (Object o : list) {
str = str + o;
}
return str;
}
/**
* 将字符写到文件中
* @param str
* @param str
*/
public static void stringWrite(String str, String fileName){
FileWriter fw = null;
try {
fw = new FileWriter(fileName);
fw.write(str);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment