将文本文件的内容写入到一个文件夹
public static void main(String[] args) {
counta1(new File("H:\\jse2301"));
}
private static void counta1(File dir) {
File[] files = dir.listFiles();
for (File f : files) {
if (f.isDirectory()) {
counta1(f);
}
if (f.isFile() && f.getName().endsWith("java")) {
appenda1(f.getAbsolutePath(), "H:\\a.txt");
}
}
}
private static void appenda1(String src, String dst) {
try (FileReader read = new FileReader(src);
BufferedReader reader = new BufferedReader(read);
FileWriter wite = new FileWriter(dst, true);
BufferedWriter writer = new BufferedWriter(wite);
) {
int row = 0;
String header = """
%n/**%n
* 文件名:%s %d行%n
* 时间:%tF %<tT%n
* 路径:%s%n
*/%n
""";
File ff = new File(src);
long rowss = new String(new FileInputStream(ff).readAllBytes()).lines().count();
writer.write(String.format(header, ff.getName(), rowss, ff.lastModified(), ff.getAbsolutePath()));
while (reader.ready()) {
writer.write(++row + " " + reader.readLine() + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
随机生成单词随机组合生成新文件
public static void main(String[] args) {
try (var writer = new BufferedWriter(new FileWriter("H:\\a.txt", true))) {
for (int i = 0; i < 5000; i++) {
writer.write(scoundworda1() + "\r\n");
}
} catch (Exception e) {
}
}
private static String scoundworda1() {
String[] sb3 = {
".", ";", "!", "?"};
Random r = new Random();
StringBuilder sb1 = new StringBuilder();
sb1.append(worda1());
sb1 = new StringBuilder(sb1.substring(0, 1).toUpperCase() + sb1.substring(1));
for (int i = 0; i < r.nextInt(2, 12); i++) {
sb1.append(" ").append(worda1());
}
sb1.append(sb3[r.nextInt(0, 4)]);
return sb1.toString();
}
private static String worda1() {
Random r = new Random();
StringBuilder sb1 = new StringBuilder();
String s1 = "q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m";
String[] s2 = s1.split(",", 26);
Arrays.sort(s2);
for (int i = 0; i < r.nextInt(2, 16); i++) {
sb1.append(s2[r.nextInt(0, 26)]);
}
return sb1.toString();
}
将一个文件分成10个文件(不建议用)
public static void main(String[] args) {
String a1 = "H:\\javaPartition\\a.wmv";
File f = new File(a1);
int block = 10;
long size = f.length();
long s = size / block + size % block;
int n = 1;
try (var is = new FileInputStream(f)) {
byte[] buf = new byte[(int) s];
int len = 0;
var os = new FileOutputStream("H:\\javaPartition\\a" + n + ".wmv");
while ((len = is.read(buf)) >= 0) {
os.write(buf, 0, len);
os.close();
++n;
if (n > block) {
break;
}
os = new FileOutputStream("H:\\javaPartition\\a" + n + ".wmv");
}
} catch (Exception e) {
}
}
将拆开的文件合并回一个
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
appenda1("H:\\javaPartition\\a\\xxa" + i + ".wmv", "H:\\javaPartition\\a\\xxa.wmv");
}
}
private static void appenda1(String src, String dst) {
appenda1(new File(src), new File(dst), 8192);
}
private static void appenda1(String src, String dst, int i) {
appenda1(new File(src), new File(dst), i);
}
private static void appenda1(File src, File dst, int i) {
byte[] buf = new byte[i];
try (var fs = new FileInputStream(src); var fd = new FileOutputStream(dst, true)) {
int len = 0;
while ((len = fs.read(buf)) >= 0) {
fd.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}
在文件分割时查看文件有没有丢字节