Develop an Algorithm and java program using Design Recipe:
1) Develop a java program to the content of one file to another. Hint 1: read() Hint 2: write()
2) Develop a java program to List all files in a given directory. Hint: list()
1.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileCopyExample {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("output.txt", true);
String s;
while ((s = br.readLine()) != null) { // read a line
fw.write(s); // write to output file
fw.flush();
}
br.close();
fw.close();
System.out.println("file copied");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.
import java.io.File;
class Main {
public static void main(String[] args) {
// creates a file object
File file = new File("C:\\Users\\Guest User\\Desktop\\Java File\\List Method");
// returns an array of all files
String[] fileList = file.list();
for(String str : fileList) {
System.out.println(str);
}
}
}
Get Answers For Free
Most questions answered within 1 hours.