Total members 11890 |It is currently Mon May 06, 2024 2:02 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





This a simple method to read and write from/in file. hope it helps

Code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileManipulator {
    private File file;
    
    
// constructor
    public FileManipulator(File file) {
        this.file = file;
    }

    public FileManipulator(String filePath) {
        file = new File(filePath);
    }
    
    
// reading method 
    public String ReadFromFile() {
        String str = "", tempStr = "";
        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader(this.file));
            while ((tempStr = br.readLine()) != null)
                str += tempStr + "\n";
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return str
;
        
    
}
    //writing method 
    public void WriteInFile(String content) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write(content);
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        FileManipulator fm = new FileManipulator("input.txt");
        String content = fm.ReadFromFile();
        System.out.println(content);
        fm.file = new File("output.txt");
        fm.WriteInFile(content);
    }

}
 





Attachments:
FileManipulator.java [1.31 KiB]
Downloaded 1072 times
Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time

Thank you so much :) .For you share .
I moved your topic to java examples :).

Have a nice time .


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : File write read
 Read and write CSV file     -  
 Read and Write to file using ASP     -  
 How to read and write to CSV file from python     -  
 Write and Read to File In Java Example     -  
 Write to file using php     -  
 Write Vector list to File     -  
 read from file in C++     -  
 Read csv file     -  
 Read Binary File in C++     -  
 javascript read file     -  



Topic Tags

Java Files and I/O
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com