BP Forums
JOptionPaneTextWriter - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19)
+---- Thread: JOptionPaneTextWriter (/showthread.php?tid=732)



JOptionPaneTextWriter - WitherSlayer - 01-17-2013

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!