Results 1 to 11 of 11
- 11-01-2009, 04:34 PM #1
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
java.util.Formatter doesnt seem to create a file for me incase I dont have one..
Hi! I was watching this tutorial on how to find a file/make a fiule incase the file doesnt exsist.
And there they used java.util.Formatter
But for me it doesnt seem to work.
You can take this code straight from here and it should give u same error.
It doesnt crete a file for me, any idewas why?
Heres my code:
PHP Code:/** * @(#)CNDF.java * * CNDF application * * @author * @version 1.00 2009/10/29 */ import javax.swing.*; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.io.*; import java.awt.event.ActionListener; import java.util.Scanner; import java.util.Vector; import java.util.*; public class CNDF{ public static void main(String[] args) throws IOException{ GameFrame window = new GameFrame(); //window. } } class GameFrame extends JFrame implements ActionListener { Vector<JButton> adds = new Vector<JButton>(); final Formatter fileFind; String Path; JTextField NameField = new JTextField(30); JTextField AmountField = new JTextField(30); JComboBox tixOrRobux = new JComboBox(); JButton ChangeAmount = new JButton("ChangeAmount"); public GameFrame() throws IOException { try{ fileFind = new Formatter("CNDF.txt"); } catch (Exception e){ System.out.println("Cant find file"); e.printStackTrace(); } Path = Locale.getDefault()+"CNDF.txt"; Scanner text = new Scanner(new FileReader(Path)); JLabel Amount2 = new JLabel("Christian Newbie Donation Foundation Cash:"); add(Amount2); JLabel Amount3 = new JLabel("0 TIX"); add(Amount3); JLabel Amount4 = new JLabel("0 ROBUX"); add(Amount4); add(ChangeAmount); ChangeAmount.addActionListener(this); int row = 0; int amountRows = 1; while (text.hasNext()){ String line = text.next(); System.out.println(line); if (row == 0){ JLabel Name = new JLabel(" "+line+" "); add(Name); } if (row==1){ JLabel Amount = new JLabel("| "+line); add(Amount); } if (row == 2){ JLabel Amount = new JLabel("| "+line); add(Amount); JButton remove = new JButton("Remove"); adds.add(remove); remove.addActionListener(this); add(remove); amountRows++; } ++row; if (row == 3){ row = 0; } } text.close(); add(NameField); tixOrRobux.addItem("Tix"); tixOrRobux.addItem("Robux"); add(AmountField); add(tixOrRobux); JButton Add = new JButton("Add"); Add.addActionListener(this); add(Add); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); System.out.print(amountRows); setLayout(new GridLayout(++amountRows,4)); //go.addActionListener(this); pack(); setVisible(true); } public void removeLine(int line) throws IOException { Scanner txt = new Scanner(new FileReader(Path)); PrintWriter txtFile = new PrintWriter(new FileWriter(Path)); int number = line*3; int x = 1; while (txt.hasNext()){ System.out.println(Integer.toString(x)+"|"+Integer.toString(x)); if (x != number-2 && x != number-1 && x != number){ String txtline = txt.next(); System.out.println(txtline); txtFile.println(txtline); } x++; } new GameFrame(); this.dispose(); } public void actionPerformed(ActionEvent evt){ System.out.println("Clicked"); boolean clicked = false; for (int x=0;x<adds.size();x++){ if (adds.get(x) == evt.getSource()){ System.out.println("you clicked button: "+Integer.toString(x)); clicked = true; try{ removeLine(x); } catch(Exception e){ e.printStackTrace(); } } } if (clicked == false && evt.getSource() != ChangeAmount){ try { Scanner text2 = new Scanner(new FileReader(Path)); PrintWriter txtFile = new PrintWriter(new FileWriter(Path,true)); //while (text2.hasNext()){ // String line = text2.next(); // System.out.println(line); // txtFile.println(line); //} txtFile.println(NameField.getText()); txtFile.println(AmountField.getText()); String s = (tixOrRobux.getSelectedItem()).toString(); txtFile.println(s); txtFile.close(); new GameFrame(); this.dispose(); } catch (Exception e){ e.printStackTrace(); } } if (evt.getSource() == ChangeAmount){ System.out.println("Change"); } } }
Oh and heres the vid:
YouTube - Java Programming Tutorial - 79 - Creating Files
- 11-01-2009, 05:14 PM #2
well sure, java.util.Formatter is only for working with the file name of the parameters you give it.
after you get the Formatter, you are pre-pending a locale onto the formatter path, so we also need to test if that file exists too.
Java Code:Formatter fileFind = null; try{ fileFind = new Formatter("CNDF.txt"); } catch (Exception e){ System.out.println("Cant find file"); e.printStackTrace(); return; } String path = Locale.getDefault()+"CNDF.txt"; if (!new File(path).exists()) { System.out.println("Cant find file: " + path); return; }
- 11-01-2009, 06:33 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Yes there you make so that incase of an error, the code will keep living.
But what I need help with is this:
The Formatter should work so that incase CNDF.txt doesnt exsist then it creates an CNDF.txt at main location.
Which is found by that Locate.getIdontRemember()
But in this case, it doesnt create a txt file..
Why?
-
What is the "main location" that you're using to look for this file?
What if you do this to find your default user directory? Could your file be located here?
Java Code:System.out.println(System.getProperties("user.dir"));
- 11-01-2009, 06:44 PM #5
I think the Formater just throws an error if the file does not exist. To have a file created when it didnt' exist,
we would need to do the File createFile() method
like, maybe invoke this instead of that try/catch exception to test if file exists with Formatter
Java Code:String path = Locale.getDefault()+"CNDF.txt"; File cndfFile = new File(path); if (!cndfFile.exists() { cndfFile.createFile(); }
-
The API says otherwise:
file - The file to use as the destination of this formatter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
- 11-01-2009, 06:53 PM #7
ah,
but that's if invoked with the Formatter(File) constructor.
the Formatter(String) throws exception if file doesn't exist.
fileName - The name of the file to use as the destination of this formatter. If the file exists then it will be truncated to zero size;
FileNotFoundException - If the given file name does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
fileFind = new Formatter("CNDF.txt");
Java Code:fileFind = new Formatter(new File("CNDF.txt"));
Last edited by travishein; 11-01-2009 at 06:58 PM.
- 11-01-2009, 06:58 PM #8
hmm, but that should still create a file
- 11-01-2009, 07:22 PM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
yes it's real weard..
It doesnt work even tho it says it should work..
- 11-01-2009, 10:30 PM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Lolz, I was sick of lua cause it had so many errors that made unlogical errors.
Is java the same?!
I seem to always run into these errors which no on can solve, java isnt bullet proof right?
Can someone like give a number, 1,10 on how reliable java really is?
Is c++ maybe more safe/have less errors?
-
Similar Threads
-
jdbc (java.util.)Properties file
By jon80 in forum JDBCReplies: 3Last Post: 04-05-2011, 08:55 AM -
Jre upgrade Issue :java.util.zip.ZipException: error in opening zip file
By selvakumar.velmurugesan in forum New To JavaReplies: 0Last Post: 10-27-2009, 06:20 AM -
dont let me create simple class
By itaipee in forum New To JavaReplies: 5Last Post: 01-11-2009, 12:07 PM -
How to create exe file in java
By radix in forum New To JavaReplies: 8Last Post: 11-06-2008, 05:17 PM -
Using java.util.Formatter
By Java Tip in forum Java TipReplies: 0Last Post: 11-16-2007, 03:29 PM
Bookmarks