File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes how to read a file in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "how to read a file in a string" Watch "how to read a file in a string" New topic
Author

how to read a file in a string

muhammad sami
Greenhorn

Joined: Feb 06, 2003
Posts: 4
i am trying to read a file in a string so that i can serach for particuler words but the problem is that the file can contain special characters like " (double quotes ) and single quotes so the program would give errors is there any way around ,can any one help in this regards,or send me a sample code
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 11862
The simplest thing would be to create a BufferedReader and read the file line by line, or character by character, aggregating to a StringBuffer that you can then turn into a String.
Why are you expecting quotes to cause a problem?
Bill


Java Resources at www.wbrogden.com
Md Fizal
Ranch Hand

Joined: Dec 23, 2002
Posts: 61
Originally posted by muhammad sami:
i am trying to read a file in a string so that i can serach for particuler words but the problem is that the file can contain special characters like " (double quotes ) and single quotes so the program would give errors is there any way around ,can any one help in this regards,or send me a sample code
Md Fizal
Ranch Hand

Joined: Dec 23, 2002
Posts: 61
Originally posted by muhammad sami:
i am trying to read a file in a string so that i can serach for particuler words but the problem is that the file can contain special characters like " (double quotes ) and single quotes so the program would give errors is there any way around ,can any one help in this regards,or send me a sample code

Try this
FileInputStream fis = new FileInputStream("a.txt");
String str="";
int i;
while(i=fis.read()!=-1)
str+= String.valueOf((char)i);
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 11862
Ewwwww! thats really inefficient.

It creates and discards String objects at the rate of one per character. This is what StringBuffer was made for.

Bill
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
Even that duplicates effort - just use the BufferedReader created from a FileReader!!!
 
 
subject: how to read a file in a string
 
Threads others viewed
StringTokenizer cannot split String with tabs?
which is better - array or arraylist??
Expression Language
File Upload Problem
files to strings
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture