Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 4:58 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Code Snippet
 Code subject: File write read
PostPosted: Fri Jan 02, 2009 11:25 pm 
Offline
Newbie
User avatar

Joined: Sun Dec 28, 2008 9:01 pm
Posts: 4
Has thanked: 0 time
Have thanks: 0 time

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 241 times
TOP
 Profile Send private message  
Reply with quote  
 Code subject: Re: File write read
PostPosted: Sat Jan 03, 2009 12:13 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time
Thank you so much :) .For you share .
I moved your topic to java examples :).

Have a nice time .

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  

 Similar topics
 Image Insert in Excel File Using POI
 ASP file structure
 Read your gmail using Java code
 Search records from text file
 error closing file matlab save
 How to write a JSP Program to manipulate Last Date visited
 read ibooks on pc
 How to download any file in jsp.
 read php file in asp and display it as html
 javascript read file

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team