Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class WSExample1
- {
- public static void main(String args[])
- {
- write("C:/dell/file.txt");
- read("C:/dell/file.txt");
- copy("C:/dell/file.txt","C:/dell/copiedfile.txt");
- //delete("C:/dell/file.txt");
- append("C:/dell/copiedfile.txt");
- }
- public static int write(String path)
- {
- try
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String str;
- BufferedWriter out = new BufferedWriter(new FileWriter(path));
- System.out.println("Enter lines of text.");
- System.out.println("Enter 'end' to quit.");
- do {
- str = br.readLine();
- if(!str.equals("end"))
- {
- out.write(str);
- out.newLine();
- }
- } while(!str.equals("end"));
- out.close();
- }
- catch(IOException ioe){
- System.out.println("Exception at write");
- }
- return 0;
- }
- public static int read(String path)
- {
- try
- {
- BufferedReader br = new BufferedReader(new FileReader(path));
- String str;
- System.out.println("The contents of the file are");
- String line = br.readLine();
- while(line!=null)
- {
- System.out.println(line);
- line = br.readLine();
- }
- br.close();
- }
- catch(IOException ioe)
- {
- System.out.println("Exception at read");
- }
- return 0;
- }
- public static int delete(String path)
- {
- try{
- File f= new File(path);
- if(f.delete())
- {
- System.out.println("file deleted");
- }
- else
- {
- System.out.println("Delete failed.");
- }
- }
- catch(Exception e){
- System.out.println("Exception at DELETE");
- }
- return 0;
- }
- public static int copy(String path1, String path2)
- {
- try
- {
- BufferedReader br = new BufferedReader(new FileReader(path1));
- BufferedWriter out = new BufferedWriter(new FileWriter(path2));
- String str;
- String line = br.readLine();
- while(line!=null)
- {
- out.write(line);
- out.newLine();
- line = br.readLine();
- }
- br.close();
- out.close();
- }
- catch(IOException ioe)
- {
- System.out.println("Exception at read");
- }
- return 0;
- }
- public static int append(String path)
- {
- try
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String str;
- BufferedWriter out = new BufferedWriter(new FileWriter(path, true )); //FileWriter(file path,boolean append )
- System.out.println("Enter lines of text to append to " + path);
- System.out.println("Enter 'end' to quit.");
- do {
- str = br.readLine();
- if(!str.equals("end"))
- {
- out.write(str);
- out.newLine();
- }
- } while(!str.equals("end"));
- out.close();
- }
- catch(IOException ioe){
- System.out.println("Exception at write");
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment