• Post Reply Bookmark Topic Watch Topic
  • New Topic

convert a java file to a string  RSS feed

 
Lin Spott
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to read a small java file into my program and convert it to a string.
The program currently works without the java.io line or the Scanner input line and just the String input = "smile";
Any tips appreciated.



 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to check out the java.nio.file.Files class.
It has some utility methods useful for reading files.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

you should move that code. The ideal length of a main method is one statement.
Have you read the Scanner documentation which tells you how to read from a file? It shows how to get longs, so all you have to do is work out which methods will give you the text.
How you use that text, now that is something you will have to work out.
 
Lin Spott
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for a quick response. I briefly looked at each link. I've files into the program, but have not been successful converting them to a string. My problem is pulling together StringBuilder or StringBuffer or appending a string. The examples I see are a method in themselves, I'm not sure how to implement. Your help is appreciated. My goal is to convert the simple java program into a string (I'll evaluate the what java puts at end of lines if/when I see it).
Thank you

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, many inconsistencies in your code.
Here is the example, what I think you're intended to do:
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you changed from Scanner to BufferedReader? For a simple text file a Scanner is usually simpler to use: and it does its own Exception handling so you can usually forget about it. You can get any Exceptions like this:-Spread your code out a bit. You should have one blank line between successive methods, for example.

Did you get that code to compile? I can see all sorts of syntax errors. I suggest you start by giving that class a File field and a constructor(s). Use the constructor(s) to initialise the File object to something sensible. Warning: you will have to do something about FileNotFoundExceptions.
You cannot write a String literal in the () like that (line 4). What you do is write the name you are giving that String parameter inside the method.
Delete = null in line 6. That bit is of no use. Keep the rest of line 6.
You can collapse lines 12 and 13
Version 1: stringBuilder.append( line ).append( ls );
Version 2: stringBuilder.append( line ).append( System.lineSeparator() );
If you use version 2 you can delete line 8. Version 2 doesn't work in Java6.

You cannot get at the StringBuilder outside the method because it is correctly a local variable. What you can do however is print the result
System.out.print(new Filereader("c:\\myFolder\\myFile.xyz").readFile());

I think you would do well to change the name of the class if you are using the old IO classes because there already is a FileReader class in the java.io package.
 
Lin Spott
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, especially Campbell's comments. I knew I had syntax errors, and I liked getting the explanations. Worth a million.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

What have you got now? is it working?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!