Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Java: Saving a txt file using FileWriter but it won't retain fortmatting!
New Posts  All Forums:Forum Nav:

Java: Saving a txt file using FileWriter but it won't retain fortmatting!

post #1 of 4
Thread Starter 
Hey guys, another quick question.

I've got this code. It grabs text from one of my set up text areas and copies it to a .txt file. Only problem is the formatting it has in the window is not retained when it is transferred into the text file. Any idea how I'd do this??

Code:
 public void actionPerformed(ActionEvent e) {
        if (e.getSource() == update) {
            int priorityValue = priority.getSelectedIndex()+ 1;
            MessageData.setPriority(id, priorityValue);
            displayMessage();
        } else if (e.getSource() == save) {

          try{
  String fileName = JOptionPane.showInputDialog("Enter file name");
  //String finalFileName = fileName.getText();
  outFile = new FileWriter(fileName +".txt",true);
  outFile.write(textArea.getText());
  outFile.close();
  }
  catch(IOException f) {
  System.err.println("File Error:" + f);
  System.exit(1);
  }
        }else if (e.getSource() == close) {
            dispose();
        }
    }
This is the code I'm using to grab the text and save it to the text area.

And this is the ode that formats it

Code:
 private void displayMessage() {
        String subject = MessageData.getSubject(id);
        if (subject == null) {
            textArea.setText("You did not enter a message in the ID in the previous field or your message does not exist!");
        } else {
            textArea.setText("Subject: " + subject);
            textArea.setText("From: " + MessageData.getSender(id));
            textArea.append("\
To: " + MessageData.getRecipient(id));
            textArea.append("\
Priority: " + MessageData.stars(MessageData.getPriority(id)));
            textArea.append("\
\
" + MessageData.getMessage(id));
        }
    }
Hope someone can help!

Thanks
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #2 of 4
Is the issue line breaks? The line break escape character is not the same across all platforms. Other than that idea I wouldn't know, I'm not great with string formatting.
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  hide details  
Reply
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  hide details  
Reply
post #3 of 4
What do you mean by formatting? The way you have done it, it should write a single line of text, probably containing the escape characters. Can you post example input/output? Also, try and read/write on a line by line basis, split the text from MessageData into an array first, just for kicks, and iterate through it, writting line by line.
PM me if you post and I do not respond.
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
post #4 of 4
Thread Starter 
Hey sorry for the late reply, I found the solution in the end but that way you suggested also helped. Thanks Rep +
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Java: Saving a txt file using FileWriter but it won't retain fortmatting!