• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

String Formatting  RSS feed

 
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently working on a project that can save text from a JTextArea and also open that same saved text. The problem I am having is that when I open the saved text file it is no longer formatted.

How the file looks before saving/closing:

public class HelloWorld{

System.out.println("Hello World!");
}

How the file looks after being saved/closed then opened:

public class HelloWorld{ System.out.println("Hello World!");}

When I save the file I'm actually saving the entire thing to a single string. When I do this the String eliminates all tabbed spacing and pushes all characters to one single line.

The only ideas I had for fixing this were to either somehow use Format in the String class or record every time user tabs and add \t to the String.

Any help out there?
 
Marshal
Posts: 24464
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's pretty clear that the problem is related to the way you are saving the data. But since you haven't showed us how you are doing that, it's hard to comment on that.

Perhaps you could show us your code?
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I let this sit for a while and just came back to it today and figured out a solution. My issue was with Saving and Opening.

Saving: I'm guessing that by saving the file directly from a String the platform takes away the New Lines when using a FileWriter wrapped with a BufferedWriter. To fix this, I just forced new lines with custom code.
Opening: When opening the file the New Lines were also removed so again I just forced new lines with custom code.

Here's the working code.



This isn't the finished product: change things like remove the Output Points and condense the number of String variables in the file.

Overall, she works and I'm happy with it.

Any constructive criticism?
 
Rancher
Posts: 3040
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use you own code to read/write the file.

Use the read(...) and write(...) methods provided by the JTextArea API. Don't reinvent the wheel.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!