Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JOptionPaneTextWriter
#1
This applet will create a text document with whatever you want to write in it!
[code2=java]import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import javax.swing.JOptionPane;
import java.applet.Applet;

public class JOptionpaneTextWriter extends Applet{

private static final long serialVersionUID = 1L;


public static void main(String[]args){
text();
}

public static void text(){
JOptionPane.showMessageDialog(null, "This is the TEXT DOCUMENT CREATOR!");//Title
String message = (JOptionPane.showInputDialog("What would you like the document to say:"));//This takes the users input
File newFile = new File("C:/Documents and Settings/The Bairns/Desktop/newtxt.txt"/*Txt means it will be saved as a notepad file*/);//Where it will be saved an the name
if (newFile.exists())//If the file alreay exists
JOptionPane.showMessageDialog(null, "The file already exists!");//Then this message will come up!
else
{
try
{
newFile.createNewFile();//This creates a new file
}
catch (Exception e)//If somethig goes wrong. But nothing will go wrong becauce it has already checked that the file doesn't exist
{
e.printStackTrace();//Tracer from Star Wars
}
try
{
FileWriter fileW = new FileWriter(newFile);
BufferedWriter buffW = new BufferedWriter(fileW);//The text writer
buffW.write(message);//The message
buffW.close();
JOptionPane.showMessageDialog(null, "File Written!");//Success
}
catch (Exception e)
{
e.printStackTrace();//Another tracer from Star Wars
}
}
}
}[/code2]
Fully commented!


Messages In This Thread
JOptionPaneTextWriter - by WitherSlayer - 01-17-2013, 07:52 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)