1、编写程序,将一个目录及其子目录下的所有 txt 类型的文本文件中的内容合并到若干个新的文本文件中,当第一个新产生的文件存储的内容达到 1M 时,剩下的内容存储到第二个新的文件中,依次往下,新产生的文本文件名依次为 1.txt、2.txt、……。
小生愚钝。如果哪位大虾有更好的方法请给我留言!
public class Demo {
private static int number = 1;
public static void main(String[] args) throws Exception {
showTxt("D://Oracle");
}
public static void showTxt(String filePath) throws Exception {
File file = new File(filePath);
if (file.isDirectory()) {
File[] directory = file.listFiles();
int len = directory.length;
for (int i = 0; i < len; i++) {
if (directory[i].isDirectory())
showTxt(directory[i].getAbsolutePath());
else {
ChargeTxt(directory[i].getAbsolutePath());
}
}
} else {
ChargeTxt(file.getAbsolutePath());
}
}
public static void ChargeTxt(String filePath) throws Exception {
File file = new File(filePath);
int index = file.getAbsolutePath().lastIndexOf(".");
String key = file.getAbsolutePath().substring(index + 1);
if ("txt".equalsIgnoreCase(key)) {
CopyTxt(file.getAbsolutePath());
}
}
public static void CopyTxt(String filePath) throws Exception {
String newFilePath = "D://新建文件夹//" + number + ".txt";
File newFile = new File(newFilePath);
// 旧文件输入流
FileInputStream fIn = new FileInputStream(filePath);
BufferedInputStream bIn = new BufferedInputStream(fIn);
// 新文件输出流
FileOutputStream fOut = new FileOutputStream(newFilePath, true);
BufferedOutputStream bOut = new BufferedOutputStream(fOut);
byte[] bytes = new byte[1024];
while (bIn.read(bytes) != -1) {
if (newFile.length() > 1024 * 1024) {
newFilePath = "D://新建文件夹//" + ++number + ".txt";
newFile = new File(newFilePath);
fOut = new FileOutputStream(newFilePath);
bOut = new BufferedOutputStream(fOut);
bOut.write(bytes);
} else {
bOut.write(bytes);
}
bOut.flush();
}
bIn.close();
bOut.close();
}
}
运行结果:
2、编写一个函数,把 StringReader 输入流中所有英文字母变成大写,并将结果写到一个 StringWriter 输出流对象中。然后用这个函数将一个字符串中的所有字符转换成大写。
public class Demo {
public static void main(String[] args) throws Exception {
StringReader sr = new StringReader("DriverKing_斌");
StringWriter sw = new StringWriter();
toUpper(sr, sw);
System.out.println(sw.toString());
}
public static void toUpper(Reader in, Writer out) throws Exception {
BufferedReader br = new BufferedReader(in);
BufferedWriter bw = new BufferedWriter(out);
StringBuffer sb = new StringBuffer();
char[] ch = new char[1024];
int len = 0;
while ((len = br.read(ch)) != -1) {
for (int i = 0; i < len; i++) {
sb.append(Character.toUpperCase(ch[i]));
}
bw.write(sb.toString());
sb.delete(0, sb.length());
bw.flush();
}
}
}
// 输出:DRIVERKING_斌
3、阅读下面程序代码:
public class Demo {
public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in,"iso8859-1");
BufferedReader br = new BufferedReader(isr);
System.out.println(br.readLine());
br.close();
}
}
// 输入:中国
// 输出:ä¸å½
输出结果为乱码,要求用两种方式修改,可以正确输出
public class Demo {
public static void main(String[] args) throws Exception {
//第一种
InputStreamReader isr = new InputStreamReader(System.in,"utf-8");
BufferedReader br = new BufferedReader(isr);
System.out.println(br.readLine());
br.close();
//第二种
InputStreamReader isr = new InputStreamReader(System.in,"iso8859-1");
BufferedReader br = new BufferedReader(isr);
System.out.println(new String(br.readLine().getBytes("iso8859-1"),"utf-8"));
br.close();
}
}
// 输入:中国
// 输出:中国