I'm making a text editor just because I feel like it, I know there are already good text editors out there and this one isn't going to be better but I still want to make one because, again, I feel like it.

I'm using JFrame to make my GUI and I was wondering how I could make it create a file when you press a button. I have the action listener and stuff to listen when your pressing the button but I don't know how to create files, I would also like to save to a file what you've written and load a file.

How do I go about doing this?

You can use the following classes:
1. File - this handles the name/path of the file
2. PrintStream - this allows you to use the print() method to send your text to the File
3. FileReader - this allows you to read in the file that you printed earlier
4. JFileChooser - (optional) prompts user for file name

There are alternatives for the input & output streams, but these will work OK for straightforward text. Also you can find lots of examples on the web.

Awesome, I can use PrintStream for saving?
File reader for loading?
File chooser to load from and save to?
How does File handle the path/name?
Could you show me an example?

1 yes
2 yes
3 yes
4 see the API documentation - it's a simple as new File("mydata.txt"), or you can use an explicit path
5 You'll find loads of examples on the web. Search: Java read write text file

Thanks again.

Well, I could only find examples of fstream.
Could you provide an example?

fstream is found in c++ and such. You obviously didn't follow my search suggestion.
Just go to Google and enter the following search:

Java read write text file

In still can't find one with your classes.
I did google 'Java read write text file'.

Yay! Thanks, i'll have a look.

I have this but I don't really know how to use it. I need it to save a JTextArea.
Know how?

Just getText() from the text area and print the resulting String to the file. To reload, read each line of the file into a String and append them to the text area.

Many thanks, how do I create a file? It comes up with unable to read file if the file doesn't already exist.

Of course you can't read a file if it doesn't exist!
You must write it first (eg PrintStream).
User starts with empty editor. Types some stuff in. Saves it (you use PrintStream to print the text to the file). Next time user can now open that file for more editing, and you can read it in.

Okay, so I'm able to save and load now, happly :) but it only saves the first line, or loads the first line. Any reason why?
I have for the load, area.setText( new DataInputStream(fin).readLine() );
maybe setText only sets the first line?

If your text has line breaks in it them print should write multiple lines to the file. readLine(), on the other hand, does exactly that - reads one line. So you need to read lines in a loop adding the lines to the text area until you have read all the file.

I see, thanks so much!
You are a Java expert!

Error on printLine():
The method printLine() is undefined for the type DataInputStream.

Did you expect to be able to print to an input stream?
I don't have your code in front of me, but it sounds like you are getting input and output mixed up.

So how would I input more than 1 line?

The link you gave me only loads the last line.

import javax.swing.*;
import java.util.*;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class apples {
	 
	public static void main(String[] args){
		tuna object = new tuna();
		final Color redc;
		Color bluec;
		
		final String p = "Type in the path of the file you want to save in.\nYou are able to type a non-existing path, Typo will automatically create the file.\nDefault Path: C:\\Typo\\type.txt\n";
		
		
		final Font comic = new Font("comic sans ms", Font.PLAIN, 16);
		final Font arial = new Font("arial", Font.BOLD, 16);
		final Font times = new Font("times new roman", Font.PLAIN, 16);
		final Font tohama = new Font("Verdana", Font.PLAIN, 16);
		final Font crack = new Font("Cracked Johnnie", Font.PLAIN, 16);
		final Font cournew = new Font("Courier New", Font.PLAIN, 16);
		final Font lucuni = new Font("Lucida Sans Unicode", Font.PLAIN, 16);
		
		object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		object.setSize(1000, 700);
		object.setVisible(true);
		JMenuBar bar = new JMenuBar();
		object.setJMenuBar(bar);
		JMenu file = new JMenu("File");
		JMenu about = new JMenu("Help");
		JMenu font = new JMenu("Font");
		JMenu fontcol = new JMenu("Font Color");
		JMenu savpath = new JMenu("Save/Load Path");
		bar.add(file);
		bar.add(font);
		bar.add(fontcol);
		bar.add(savpath);
		bar.add(about);
		final JTextArea area = new JTextArea();
		JMenuItem load = new JMenuItem("Load File");
		final JMenuItem save = new JMenuItem("Save File");
		final JMenuItem exit = new JMenuItem("Exit");
		final JMenuItem helpi = new JMenuItem("Not Saving/Loading");
		JScrollPane scroll = new JScrollPane();
		JMenuItem com = new JMenuItem("Comic Sans MS");
		JMenuItem arial1 = new JMenuItem("Arial");
		JMenuItem times1 = new JMenuItem("Times New Roman");
		JMenuItem toha = new JMenuItem("Verdana");
		JMenuItem cracki = new JMenuItem("Cracked Johnnie");
		JMenuItem cour = new JMenuItem("Courier New");
		JMenuItem luc = new JMenuItem("Lucida Sans Unicode");
		final JMenuItem redi = new JMenuItem("Red");
		JMenuItem bluei = new JMenuItem("Blue");
		object.add(area);
		area.add(scroll);
		fontcol.add(redi);
		fontcol.add(bluei);
		file.add(load);
		file.add(save);
		file.add(exit);
		font.add(arial1);
		font.add(cracki);
		font.add(com);
		font.add(cour);
		font.add(times1);
		font.add(toha);
		final String path = "C:\\Typo\\type.txt";
		redc = Color.red;
		
		font.add(luc);
		JMenuItem made = new JMenuItem("Who made this?");
		about.add(made);
		about.add(helpi);
		JMenuItem savpathi = new JMenuItem("Change Saving and Loading Path");
		savpath.add(savpathi);
		
		class pathact implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				String path = JOptionPane.showInputDialog(p);
				
			}
			
		}
		class savea implements ActionListener{
			public void actionPeformed(ActionEvent e){
				FileOutputStream fout;		

				try
				{
				    
				    fout = new FileOutputStream (path);

				    
				    new PrintStream(fout).println (area.getText());
				    JOptionPane.showMessageDialog(null, "Saved.");
				    
				    fout.close();		
				}
				
				catch (IOException e1)
				{
					JOptionPane.showMessageDialog(null, "Failed to save to file. \nPlease create a folder on your C drive called Typo.");
					
				}
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				
				
				FileOutputStream fout;
				try
				{
				    
				    fout = new FileOutputStream (path);

				    
				    new PrintStream(fout).println (area.getText());
				    JOptionPane.showMessageDialog(null, "Saved.");
				    
				    fout.close();		
				}
				
				catch (IOException e1)
				{
					JOptionPane.showMessageDialog(null, "Failed to save to file. \nPlease create a folder on your C drive called Typo.");
					
				}
				
			}
		}
		class load implements ActionListener{
			public void actionPeformed(ActionEvent e){
				
					
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				FileInputStream fin;		

				try
				{
					FileInputStream fstream = new FileInputStream(path);
				    // Get the object of DataInputStream
				    DataInputStream in = new DataInputStream(fstream);
				        BufferedReader br = new BufferedReader(new InputStreamReader(in));
				    String strLine;
				    //Read File Line By Line
				    while ((strLine = br.readLine()) != null)   {
				      // Print the content on the console
				      area.setText (strLine);
				    }
				    //Close the input stream
				    in.close();
				    }catch (Exception e1){//Catch exception if any
				      JOptionPane.showMessageDialog(null, "Error: " + e1.getMessage());
				    }
				
			}
		}
		class madeaction implements ActionListener{
			public void actionPeformed(ActionEvent e){
				
				JOptionPane.showMessageDialog(null, "Notes is developed by Kiefer Lam. This program is made by Java.");
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Typo was first developed in 2010 by Kiefer Lam.\nHe is currently (Year 2010) 12 years old.\nThis program is made with JAVA.");
				
			}
		}
		class exitaction implements ActionListener{
			public void actionPeformed(ActionEvent e){
				System.exit(0);
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
				
			}
		}
		class comact implements ActionListener{
			public void actionPeformed(ActionEvent e){
				area.setFont(comic);
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				area.setFont(comic);
				
			}
		}
		class aract implements ActionListener{
			public void actionPeformed(ActionEvent e){
				area.setFont(arial);
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				area.setFont(arial);
				
			}
		}
		class timeact implements ActionListener{
			public void actionPeformed(ActionEvent e){
				area.setFont(times);
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				area.setFont(times);
				
			}
		}
		class tohact implements ActionListener{
			public void actionPeformed(ActionEvent e){
				area.setFont(tohama);
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				area.setFont(tohama);
				
			}
		}
		class cract implements ActionListener{
			public void actionPeformed(ActionEvent e){
				area.setFont(crack);
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				area.setFont(crack);
				
			}
		}
		class couract implements ActionListener{
			public void actionPeformed(ActionEvent e){
				area.setFont(cournew);
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				area.setFont(cournew);
				
			}
		}
		class lucact implements ActionListener{
			public void actionPeformed(ActionEvent e){
				area.setFont(lucuni);
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				area.setFont(lucuni);
				
			}
		}
		class redact implements ActionListener{
			public void actionPeformed(ActionEvent e){
				JOptionPane.showMessageDialog(null, "This feature isn't available yet.\nPlease wait for the next release of Typo.");
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				area.setForeground(Color.red);
				
			}
		}
		class blueact implements ActionListener{
			public void actionPeformed(ActionEvent e){
				JOptionPane.showMessageDialog(null, "This feature isn't available yet.\nPlease wait for the next release of Typo.");
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				area.setForeground(Color.blue);
				
			}
		}
		class savhact implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Problem:\n•Unable to read from file\nSolution: Create a folder and name it 'Typo' wihtout the quotes in the C drive.\n•");
				
			}
			
		}
		
		bluei.addActionListener(new blueact());
		savpathi.addActionListener(new pathact());
		redi.addActionListener(new redact());
		helpi.addActionListener(new savhact());
		luc.addActionListener(new lucact());
		cour.addActionListener(new couract());
		cracki.addActionListener(new cract());
		toha.addActionListener(new tohact());
		times1.addActionListener(new timeact());
		arial1.addActionListener(new aract());
		com.addActionListener(new comact());
		exit.addActionListener(new exitaction());
		made.addActionListener(new madeaction());
		load.addActionListener(new load());
		save.addActionListener(new savea());
	}

	
}

You are using setText in the loop, so each line replaces the one before. You need to add the text on to the end of the existing text each time.

How do I add the text onto the existing text?

What have you tried? Have you read the API doc for JTextArea?

Actually I haven't tried, I have no idea how.
I've read the JTextArea API but I don't really take in what I see unless I'm really motivated.

Well, it's your program. How much more motivation do you need? Read the available methods for the JTextArea and see if any are available to add text.

Honestly, if you don't learn to effectively get this information from the API docs, your use of Java is going to be quite hobbled and difficult. I know it's a lot to wade into, but taking the time to learn how to use them well will definitely pay off.

Ezzaral is right. IMHO the ability to find and understand stuff in the API doc is as essential as knowing how to program a loop.
You are looking for a method to append a String to a JTextArea. I promise you this is not hard to find or understand.

OK, I'll try and remember this stuff that I find out from the API docs then.

Listen, I wouldn't try too hard to remember the details of the API - it's so huge that nobody tries to learn it all. The important thing is to get good at finding the stuff when you need it.
You'll get to know the most common stuff anyway just by using it. For the rest it's good enough just to remember "wasn't there a method in the XYZ class to do something like that?" then look it up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.