java中io练习_Java IO 练习

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();

}

}

运行结果:

0818b9ca8b590ca3270a3433284dd417.png

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();

}

}

// 输入:中国

// 输出:中国

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值