Writing to files


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Writing to files

  1. #1
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108

    Unhappy Writing to files

    I am writing a string of text to a file, but I am having problems with getting the text to a new line. I think I have seen how this can be done in C++ where you check the string for "\n" in a loop and then make it write a new line. Does anyone have any ideas about how I can get this done??

    Thanks in advace

  2. #2
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Here's an example:
    Code:
    import java.io.*;
    
    public class WriteToFile {
    
        public static void main(String[] args) {
            String[] s = {"line 1", "line 2", "line 3"};
            write(s, "out.txt");
        }
    
        public static void write(String[] lines, String file) {
            try {
            	FileWriter writer = new FileWriter(file);
            	BufferedWriter buffer = new BufferedWriter(writer);
            	for(int i = 0; i < lines.length; i++) {
                	buffer.write(lines[i]);
                	buffer.newLine();
                }
            	buffer.close();
            	writer.close();
            }
    		catch(Exception e) { e.printStackTrace(); }
        }
    }
    Good luck.

  3. #3
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    I don't know how I could implement this in my program because, what I do is I get a string from a JTextField and I write that out. It is not an array.


    Here is my code

    Code:
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    class Writer extends JFrame
    {
    	public Writer()
    	{
    		super("File Writer");
    		
    		Container holder = getContentPane();
    		final JTextArea  text = new JTextArea();
    		JButton save = new JButton("Save it");
    		
    		
    		
    		holder.setLayout(new BorderLayout());
    		holder.add(text, BorderLayout.CENTER);
    		holder.add(save, BorderLayout.SOUTH);
    		
    		save.addActionListener(new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					File outputFile;
    					FileWriter out;
    					BufferedWriter writer;
    					
    					JFileChooser chooser = new JFileChooser();
    					
    					int option = chooser.showSaveDialog(null);
    					
    					if(option == JFileChooser.APPROVE_OPTION)
    					{
    						setTitle(chooser.getSelectedFile().toString());
    						outputFile = new File(chooser.getSelectedFile().toString());
    
    						try
    						{
    							out = new FileWriter(outputFile);
    							writer = new BufferedWriter(out);
    							writer.write(text.getText());
    							writer.close();
    						}
    						catch(Exception exc){}
    						
    					}
    				}
    			}
    		);
    		
    		setSize(300,250);
    		setVisible(true);
    	}
    	
    	public static void main(String args[])
    	{
    		Writer  riter = new Writer();
    		
    	}
    }
    Last edited by Wizard1988; 09-30-2005 at 07:22 AM.

  4. #4
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    i guess you want to split your text into strings
    you can use StringTokenizer class ( now legacy )
    but there is another option java document advice

    Code:
    JTextArea text = new JTextArea();
        text.setText("this is a test");
        String[] result = text.getText().split("\\s");
        for ( int i = 0 ; i < result.length ; i++ ){
          System.out.print(result[i]);
          System.out.println();// writeln() does this..

Similar Threads

  1. Reading & Writing Binary files
    By chris3113 in forum VB Classic
    Replies: 2
    Last Post: 08-28-2005, 07:00 AM
  2. Replies: 7
    Last Post: 06-23-2005, 10:39 PM
  3. recurrence writing files on the disk
    By wakum in forum Java
    Replies: 4
    Last Post: 04-29-2005, 08:10 AM
  4. Writing Fixed len. Text Files
    By Harrison in forum VB Classic
    Replies: 1
    Last Post: 11-30-2000, 02:56 PM
  5. Writing to @.txt/@.asc files
    By jj in forum VB Classic
    Replies: 1
    Last Post: 07-07-2000, 05:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center