-
【简】设计两个⽅法,分别实现拷⻉⼀个⽂本⽂件(分别⽤字节流和字符流实 现)
public class Work1 {
public static void main(String[] args) {
copyFileByByte("C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\source.txt",
"C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\copy1.txt"
);
copyFileByChar("C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\source.txt",
"C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\copy2.txt"
);
}
public static void copyFileByByte(String start,String target){
try(InputStream in = new FileInputStream(start); OutputStream out = new FileOutputStream(target)){
int available = in.available();
byte[] bytes = new byte[available];
in.read(bytes);
System.out.println(new String(bytes));
out.write(bytes);
}catch (Exception e){
e.printStackTrace();
}
}
public static void copyFileByChar(String start,String target){
try(Reader reader = new FileReader(start); Writer writer = new FileWriter(target)){
char[] chars = new char[16];
int len;
while ((len=reader.read(chars))!=-1){
writer.write(chars,0,len);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
-
读取⼀个⽂本中的内容,将所有的⼤写字⺟转成⼩写,⼩写字⺟转成⼤ 写,写⼊到另外⼀个⽂本
public class Work2 {
public static void main(String[] args) {
letterReverse("C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\source.txt",
"C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\copy3.txt"
);
}
public static void letterReverse(String start,String target){
try(Reader reader = new FileReader(start); Writer writer = new FileWriter(target);) {
int t;
while ((t=reader.read())!=-1){
if(t>='a'&&t<='z'){
writer.write(t-32);
}else if(t>='A'&&t<='Z'){
writer.write(t+32);
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
-
取100个随机数,写⼊到本地⽂件中,加序号,每写⼀个换⾏。
public class Work3 {
public static void main(String[] args) {
randomToFile("C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\work3.txt");
}
public static void randomToFile(String target){
try(Writer writer = new FileWriter(target);) {
Random random = new Random();
StringBuffer stringBuffer = new StringBuffer();
for (int i = 1; i <= 100; i++) {
stringBuffer.append(i+":"+random.nextInt(100)+"\n");
writer.write(stringBuffer.toString().toCharArray());
stringBuffer.delete(0,stringBuffer.length());
}
}catch (Exception e){
e.printStackTrace();
}
}
}
-
将java中所有的字符,写⼊本地⽂件,每100个⼀⾏。0-65535个
public class Work4 {
public static void main(String[] args) {
allCharToFile("C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\work4.txt");
}
public static void allCharToFile(String target){
try(Writer writer = new FileWriter(target);) {
writer.write(0);
for (int i = 1; i <= 65535; i++) {
writer.write(i);
if(i%100==0){
writer.write("\n");
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
-
将三个java⽂件的内容写⼊到⼀个⽂件中
public class Work5 {
public static void main(String[] args) {
addFiles("C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\copy1.txt",
"C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\copy2.txt",
"C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\copy3.txt",
"C:\\D\\bigData\\code\\demo1\\bigdataDay18\\src\\com\\qf\\resource\\work5.txt"
);
}
public static void addFiles(String start1,String start2,String start3,String target){
try(InputStream in1 = new FileInputStream(start1); OutputStream out = new FileOutputStream(target);
InputStream in2 = new FileInputStream(start2); InputStream in3 = new FileInputStream(start3);) {
int available = in1.available();
byte[] bytes1 = new byte[available];
in1.read(bytes1,0,available);
out.write(bytes1);
available = in2.available();
byte[] bytes2 = new byte[available];
in2.read(bytes2,0,available);
out.write(bytes2);
available = in3.available();
byte[] bytes3 = new byte[available];
in3.read(bytes3,0,available);
out.write(bytes3);
}catch (Exception e){
e.printStackTrace();
}
}
}