根据小写26个字母,随机生成单词,乱文
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
public class OutputStreamDemo {
public static void main(String[] args) {
Random rand = new Random();
try (FileOutputStream fos = new FileOutputStream("木.txt")){
for (int i = 0; i < 100; i++) {
fos.write(randStr(rand.nextInt(2,20)).concat("\t\n").getBytes());
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String randStr(int n) {
Random rand = new Random();
String letter = "abcdefghijklmnopqrstuvwsyz";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
StringBuilder sb1 = new StringBuilder();
int len = rand.nextInt(2,10);
for (int j = 0; j < len; j++) {
sb1.append(letter.charAt(rand.nextInt(letter.length())));
}
sb1.append(" ");
String ts = new String(sb1);
if (i ==0 ){
ts = ts.substring(0,1).toUpperCase().concat(ts.substring(1));
}else if (i == n-1){
ts = ts.substring(0,ts.length()-1)+"!.?".charAt(rand.nextInt(3));
}
sb.append(ts);
}
return sb.toString() ;
}
}
识别一个文件的真实格式
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
public class Ex3 {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put(".jpg","ffd8ffe0");
map.put(".7z","377abcaf");
map.put(".zip","504b34");
map.put(".txt","aced05");
System.out.println(map.containsKey(".7z"));
System.out.println(getHeader(new File("D:\\IOTest\\112\\112.7z"), 4));
String type = getType(new File("D:\\IOTest\\112\\112.7z"));
System.out.println(type);
}
public static String getType(File file){
HashMap<String, String> map = new HashMap<>();
map.put("ffd8ffe0",".jpg");
map.put("377abcaf",".7z");
map.put("504b34",".zip");
map.put("aced05",".txt");
String header = getHeader(file, 4);
if (map.containsKey(header)){
return map.get(header);
}else {
return "未知文件";
}
}
public static String getHeader(File file,int n){
String header = "";
if (file.isFile()){
try (FileInputStream fis = new FileInputStream(file)){
byte[] bytes = new byte[n];
fis.read(bytes);
StringBuilder sb = new StringBuilder();
for (byte byt : bytes){
sb.append(String.format("%x",byt));
}
header = sb.toString();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return header;
}
}
指定一个目录下所有的java文件,把里面的内容格式化输出在md文件
import java.io.*;
public class AllCodeDemo {
static int sum= 0 ;
public static void main(String[] args) {
all(new File("D:\\peixun\\java\\Lx"));
}
public static void all(File file){
if (file.isDirectory()){
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isDirectory()) all(file1);
if (file1.isFile()&&file1.getName().endsWith(".java")){
all(++sum,file1,new File("木.md"));
}
}
}
if (file.isFile()&&file.getName().endsWith(".java")){
all(++sum,file,new File("木.md"));
}
}
public static void all(int n, File src,File dst){
String template = """
%d %s (%d行 %tF %<tT)
```java
%s
```
""";
try (FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dst,true)){
sum = n;
String path = src.getAbsolutePath();
String s = new String(fis.readAllBytes());
long count = s.lines().count();
long l = src.lastModified();
String format = String.format(template, sum, path, count, l, s);
fos.write(format.getBytes());
fos.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}