Reversing the contents of a file

need java program to reverse contents of file

Page 1 of 1

1 Replies - 3656 Views - Last Post: 21 February 2009 - 08:10 PM Rate Topic: -----

#1 thecat66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-February 09

Reversing the contents of a file

Post icon  Posted 21 February 2009 - 08:08 PM

// I am able to get the contents to print to console, 
// have tried some different code from web with no luck trying to do strline.reverse()

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Reverse 
{
   public static void main(String[] args)
  {
	  try{
	// Open the file that is the first 
	// command line parameter
	FileInputStream fstream = new 
	FileInputStream("C:\\Users\\Phillip\\workspace\\Part-1 Lectures\\src\\Reverse.java");
	// Get the object of DataInputStream
	DataInputStream in = new DataInputStream(fstream);
	BufferedReader br = new BufferedReader(new InputStreamReader(in));
	String strLine;
	//Read File Line By Line
	while ((strLine = br.readLine()) != null)   {
	  // Print the content on the console
		System.out.println (strLine);
		
		//System.out.println("File Reversed is" +strLine.reverse);
		
		
		
	  
	}
	//Close the input stream
	in.close();
	}catch (Exception e){//Catch exception if any
	  System.err.println("Error: " + e.getMessage());
	}
  }
}


*Edited to correct to [ code][ /code] tags

This post has been edited by pbl: 21 February 2009 - 08:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reversing the contents of a file

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reversing the contents of a file

Posted 21 February 2009 - 08:10 PM

The easiest way to reverse a String ?
Put it into a a StringBuffer.... which has a reverse() method

This post has been edited by pbl: 21 February 2009 - 08:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1