1、利用缓冲流,采用不同方式 ,实现图片视频的复制功能,并记录时间 比较效率。
import java.io.*;
public class BufferedCopy {
public static void main(String[] args) throws IOException {
String src = "d:/fine.mp4"; //复制的源文件名
String dst = "d:/fineCopy.mp4"; //复制的目的文件名
copyImg(src, dst);
copyImg2(src,dst);
}
//单个字符复制
public static void copyImg(String src, String dst) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(src)));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(dst)));
int readData;
long start = System.currentTimeMillis();
while ((readData = in.read()) != -1) {
out.write(readData);
out.flush();
}
long end = System.currentTimeMillis();
System.out.println("单个字符复制视频需要" + (end - start) + "ms");
in.close();
out.close();
}
//多个字符复制
public static void copyImg2(String src, String dst) throws IOException {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(src)));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(dst)));
int readCount;
byte[] bytes = new byte[4096];
long start = System.currentTimeMillis();
while ((readCount = in.read(bytes)) != -1) {
out.write(bytes,0,readCount);
}
long end = System.currentTimeMillis();
System.out.println("按数组复制视频需要" + (end - start) + "ms");
in.close();
out.close();
}
}
2、有这样的一个words数组,数组中每个字符串的格式为“词性:单词”。
String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
根据单词性质动词verb全部存入verb.txt文件中;
根据单词性质名词noun全部存入noun.txt文件中。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WordsCopy {
public static void main(String[] args) throws IOException {
String[] words = {"verb:eat", "verb:drink", "verb:sleep", "verb:play",
"noun:rice", "noun:meat", "noun:hand", "noun:hair"};
saveWords(words);
}
private static void saveWords(String[] words) throws IOException {
FileOutputStream verbOut = new FileOutputStream(new File("verb.txt"), true);
FileOutputStream nounOut = new FileOutputStream(new File("noun.txt"), true);
for (int i = 0; i < words.length; i++) {
char[] chs = new char[6];
words[i].getChars(5, words[i].length(), chs, 0);
String str = String.valueOf(chs)+"\n";
if (words[i].contains("verb")) {
verbOut.write(str.getBytes());
} else if (words[i].contains("noun")) {
nounOut.write(str.getBytes());
}
}
verbOut.close();
nounOut.close();
}
}
3、递归查找指定目录中(包括子目录中),所有的.java文件,并且,把所有这些找到的java文件,复制(是复制不是移动)到一个指定的目录下。
目录结构为如下:
firstLevel目录中,包含一个secondLevel目录和a1.txt和b1.java文件;
secondLevel目录中包含dir1和dir2两个目录,和a2.txt和b2.java文件;
dir1目录中包含a3.txt和b3.java文件;
dir2目录是一个空目录;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class QueryFile {
public static void main(String[] args) throws IOException {
String src = "D:\\firstLevel";
String dst = "D:\\copy\\";
copyFile(src, dst);
}
/**
* 1, 获取当前层下的所有文件及文件夹
* 2, 判断每个元素是文件还是文件夹
* 3, 文件则判断是否是.java文件,是就加入 List
* 4, 文件夹则递归调用 queryFile(文件夹目录)
*
* @param path
* @param javaFiles
*/
public static void queryFile(String path, List javaFiles) {
File dir = new File(path);
File[] files = dir.listFiles();
if (files == null || files.length == 0) return;
for (File file : files) {
if (file.isFile()) {
if (file.getName().endsWith("java")) {
System.out.println(file.getName());
javaFiles.add(file);
}
} else if (file.isDirectory()) {
queryFile(file.getAbsolutePath(), javaFiles);
}
}
}
public static void copyFile(String src, String dst) throws IOException {
List javaFiles = new ArrayList<>();
queryFile(src, javaFiles);
System.out.println(javaFiles.size());
byte[] bytes = new byte[2048];
int readCount;
for (File file : javaFiles) {
FileOutputStream out = new FileOutputStream(dst + file.getName());
FileInputStream in = new FileInputStream(file);
while ((readCount = in.read(bytes)) != -1) {
out.write(bytes, 0, readCount);
System.out.println(readCount);
}
in.close();
out.close();
}
}
}
4、从磁盘上读取一个文本文件(如某个java源代码),分别统计出文件中英文字母、空格、数字字符的个数。(文本文件中的数字指的是数字字符)
import java.io.*;
import java.util.Arrays;
public class CountCharacter {
public static void main(String[] args) throws IOException {
File file = new File("a.txt");
int[] count = count(file);
System.out.println("字母有 "+count[0]+" 个。");
System.out.println("空格有 "+count[1]+" 个。");
System.out.println("数字有 "+count[2]+" 个。");
}
public static int[] count(File file) throws IOException {
int[] cnt = new int[3];
Arrays.fill(cnt, 0);
InputStreamReader in = new InputStreamReader(new FileInputStream(file));
char[] chars = new char[1024];
while (in.read(chars) != -1) {
for (int i = 0; i < chars.length; i++) {
if (chars[i] >= 'a' && chars[i] <= 'z' || chars[i] >= 'A' && chars[i] <= 'Z') {
cnt[0]++;
} else if (chars[i] == ' ') {
cnt[1]++;
} else if (chars[i] >= '0' && chars[i] <= '9') {
cnt[2]++;
}
}
}
in.close();
return cnt;
}
}
5、在一个磁盘的文件里保存26个英文小写字母(乱序),将他们读入内存中,进行排序,把排好顺序的数再重新追加写到磁盘的该文件中。
import java.io.*;
import java.util.Arrays;
public class RWAndSort {
public static void main(String[] args) throws IOException{
File file = new File("b.txt");
reloadCharacter(file);
}
public static void reloadCharacter(File file) throws IOException {
FileReader fileReader = new FileReader(file);
char[] chars = new char[27];
fileReader.read(chars);
sortCharacter(chars);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(chars);
fileWriter.close();
fileReader.close();
}
public static char[] sortCharacter(char[] chars) {
Arrays.sort(chars);
return chars;
}
}