在JAVA中实现文件读写练习
2021-05-03 22:29
标签:创建 fileinput buffere target void except row ++ pat 练习1: 练习2: 在JAVA中实现文件读写练习 标签:创建 fileinput buffere target void except row ++ pat 原文地址:https://www.cnblogs.com/debug-the-heart/p/13196729.html有这样的一个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文件中 1 package readandwrite;
2 /*1.有这样的一个words数组,数组中每个字符串的格式为“词性:单词”
3 String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
4 根据单词性质动词verb全部存入verb.txt文件中
5 根据单词性质名词noun全部存入noun.txt文件中
6
7 */
8
9 import java.io.*;
10
11
12 public class FileReadAndWrite {
13 public static void main(String args[]) throws IOException {
14 //WORDS数组
15 String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
16 FileOutputStream outFile1 = new FileOutputStream("verb.txt");
17 FileOutputStream outFile2 = new FileOutputStream("noun.txt");
18 OutputStream out1 = new BufferedOutputStream(outFile1);
19 OutputStream out2 = new BufferedOutputStream(outFile2);
20
21 for(int i=0;i
递归查找指定目录中(包括子目录中),所有的.java文件,
并且,把所有这些找到的java文件,复制到一个指定的目录下;
注意:复制文件包括文件内容* package readandwrite;
import java.io.*;
public class FileRandW02 {
public static void main(String args[]) throws IOException {
findFile(new File("G:\\firstLevel"));
}
public static void copyFile(File sourcePath, File targetPath,String fileName) throws IOException {
FileInputStream inFile = new FileInputStream(sourcePath);
InputStream inread = new BufferedInputStream(inFile);
String add="\\";
String finalWritePath = targetPath.getPath()+add+fileName;
FileOutputStream ouFile = new FileOutputStream(finalWritePath);//会自动创建,字节流支持任意格式文件
OutputStream outwrite = new BufferedOutputStream(ouFile);
byte[] buf = new byte[4096];
int len;
while((len=inread.read(buf))!=-1){
outwrite.write(buf, 0, len);
}
inread.close();
outwrite.close();
}
public static void findFile(File currentDir) throws IOException {
if(currentDir.delete()) return;
File [] curDirFile = currentDir.listFiles();
String fileName ;
for(int i=0;i