|
| 05 Dec 2011 04:44 PM |
So I made this program to test writing to a file. When I run it, I get this error:
fileTest.java:3: illegal escape character
I'm new to writing to files. Can anyone find the problem?
----------
class fileTest { public static void main(String args[]) { FileOutputStream file = new FileOutputStream("C:\Menus\MenuTest.txt"); file.write("testing 123"); file.close(); } }
## don't judge me ## |
|
|
| Report Abuse |
|
|
Merely
|
  |
| Joined: 07 Dec 2010 |
| Total Posts: 17266 |
|
|
| 05 Dec 2011 04:49 PM |
I believe you need to escape the backslash character, because your program thinks that '\M' represents a custom character.
"C:\\Menus\\MenuTest.txt" |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 05 Dec 2011 04:50 PM |
Change
FileOutputStream file = new FileOutputStream("C:\Menus\MenuTest.txt");
to
FileOutputStream file = new FileOutputStream("C:\\Menus\\MenuTest.txt");
---------- ~ pwendu46, the unicorn ~
|
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 05 Dec 2011 04:51 PM |
Latetoasted! D:
Merely is right.
---------- ~ pwendu46, the unicorn ~ |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 05 Dec 2011 04:53 PM |
Also, change
public static void main(String args[]) {
to
public static void main(String args[]) throws IOException {
or you'll get an error message: unreported exception java.io.IOException; must be caught or declared to be thrown
---------- ~ pwendu46, the unicorn ~ |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 05 Dec 2011 04:54 PM |
| Well, sorry I don't have a Java Compiler... |
|
|
| Report Abuse |
|
|
| |
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 05 Dec 2011 04:58 PM |
^
:upthis:
---------- ~ pwendu46, the unicorn ~ |
|
|
| Report Abuse |
|
|
|
| 05 Dec 2011 05:13 PM |
What do you mean by "escape the backslash?"
## don't judge me ## |
|
|
| Report Abuse |
|
|
|
| 05 Dec 2011 05:19 PM |
Ok I changed it to this:
import java.io.*; import java.io.FileOutputStream; class fileTest { public static void main(String args[]) throws IOException { FileOutputStream file = new FileOutputStream("C:\\Menus\\MenuTest.txt"); file.write("loltest"); //line 7 file.close(); } }
And compiler says this:
fileTest.java:7: cannot find symbol
Can I only write byte characters?
## don't judge me ## |
|
|
| Report Abuse |
|
|
Merely
|
  |
| Joined: 07 Dec 2010 |
| Total Posts: 17266 |
|
|
| 05 Dec 2011 05:20 PM |
Usually when \ is followed by another character, it means a special character.
For example, \n is the new line character.
s = "sdj\nhi" print(s) --> sdj --> nhi
But if you put backslash in front of a backslash, that means that you don't want the backslash to have any special meanings. It's just a normal backslash. |
|
|
| Report Abuse |
|
|
Merely
|
  |
| Joined: 07 Dec 2010 |
| Total Posts: 17266 |
|
|
| 05 Dec 2011 05:21 PM |
I think that code about throwing an IO exception isn't necessary.
Also, my output should have been --> hi |
|
|
| Report Abuse |
|
|
Merely
|
  |
| Joined: 07 Dec 2010 |
| Total Posts: 17266 |
|
|
| 05 Dec 2011 05:22 PM |
| Never mind about the IO exception code. I suppose it is necessary. |
|
|
| Report Abuse |
|
|
|
| 05 Dec 2011 05:41 PM |
Is there any way I can convert a string to bytes? Because I tried this:
file.write(123)
and it put { in the file, which makes me imply that it can only write bytes.
## don't judge me ## |
|
|
| Report Abuse |
|
|
Merely
|
  |
| Joined: 07 Dec 2010 |
| Total Posts: 17266 |
|
|
| 05 Dec 2011 06:05 PM |
file.write( String.valueOf(123));
I found this by doing a simple web search. I suggest you learn how to do this, as this forum isn't the gathering place for Java experts. |
|
|
| Report Abuse |
|
|
|
| 05 Dec 2011 06:57 PM |
I have been searching, I just don't trust that the results are accurate. Now I have another problem: I created a program to generate a random menu for a month, which creates and writes to a text file. When I compile and run it with the argument December, I get this error that was caught by the program:
Exception in thread "main" java.io.FileNotFoundException: C:\Menus\Menu_for_Dece mber.txt (The system cannot find the path specified) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at GenerateMenu.main(menugenerator.java:21)
Here is my program, if anyone can find the error:
----------
import java.io.FileOutputStream; import java.io.File; import java.io.*; class GenerateMenu { public static void main(String args[]) throws IOException { String meals[] = {"Chicken stew", "Egg sandwiches", "Scrambled eggs", "Italian macaroni stew", "Tacos", "Leftovers", "Pepper steak", "Steamed cheese burgers", "Baked chicken on bone", "Chicken chili", "Rotisserie chicken", "Barbeque chicken", "Chicken parmigiana", "Turkey breast", "Spaghetti/pasta", "Steak", "Fish", "Pork chops", "Pork roast", "Ribs", "Shrimp", "Turkey burgers", "Home made pizza", "Hot dogs", "Ravioli", "Frozen dinners", "Pizza takeout", "Scallops"}; String sides[] = {"Brown rice", "White rice", "Baked potato", "Mashed potato", "French fries", "Pasta", "Roasted potatoes", "Sweet potatoes"}; String vegies[] = {"Corn", "Broccoli", "Carrots", "Asparagus", "Lima beans", "Baked beans"}; String month = args[0]; File folder = new File("C:\\Menus"); File men = new File("C:\\Menus\\Menu_for_" + month + ".txt"); FileOutputStream menu = new FileOutputStream("C:\\Menus\\Menu_for_" + month + ".txt"); String menuTitle = "-Dinner Menu for Month of " + month + "-"; menu.write(menuTitle.getBytes()); String weekOne = "Week 1:\n"; menu.write(weekOne.getBytes()); for (int i = 1; i < 8; i++) { int entree = (int) (Math.random() * meals.length); int side = (int) (Math.random() * sides.length); int vegie = (int) (Math.random() * vegies.length); String week = "Day " + i + ": " + "\nEntree: " + meals[entree] + ", Side: " + sides[side] + ", Vegetable: " + vegies[vegie] + "\n\n"; menu.write(week.getBytes()); } System.out.println("Week 2:\n"); for (int i = 1; i < 8; i++) { int entree = (int) (Math.random() * meals.length); int side = (int) (Math.random() * sides.length); int vegie = (int) (Math.random() * vegies.length); String weekTwo = "Day " + i + ": " + "\nEntree: " + meals[entree] + ", Side: " + sides[side] + ", Vegetable: " + vegies[vegie] + "\n\n"; menu.write(weekTwo.getBytes()); } System.out.println("Week 3:\n"); for (int i = 1; i < 8; i++) { int entree = (int) (Math.random() * meals.length); int side = (int) (Math.random() * sides.length); int vegie = (int) (Math.random() * vegies.length); String weekThree = "Day " + i + ": " + "\nEntree: " + meals[entree] + ", Side: " + sides[side] + ", Vegetable: " + vegies[vegie] + "\n\n"; menu.write(weekThree.getBytes()); } System.out.println("Week 4:\n"); for (int i = 1; i < 8; i++) { int entree = (int) (Math.random() * meals.length); int side = (int) (Math.random() * sides.length); int vegie = (int) (Math.random() * vegies.length); String weekFour = "Day " + i + ": " + "\nEntree: " + meals[entree] + ", Side: " + sides[side] + ", Vegetable: " + vegies[vegie] + "\n\n"; menu.write(weekFour.getBytes()); } menu.close(); } }
## don't judge me ## |
|
|
| Report Abuse |
|
|
| |
|
| |
|
Cenort
|
  |
| Joined: 20 Nov 2009 |
| Total Posts: 9679 |
|
|
| 05 Dec 2011 07:26 PM |
| Are you sure you need to replace ' ' with '_' for file paths? |
|
|
| Report Abuse |
|
|
|
| 05 Dec 2011 07:26 PM |
@Cen
I'm just used to doing that.
## don't judge me ## |
|
|
| Report Abuse |
|
|
Cenort
|
  |
| Joined: 20 Nov 2009 |
| Total Posts: 9679 |
|
|
| 05 Dec 2011 07:27 PM |
| Well try using spaces instead of underscores, and see what happens. |
|
|
| Report Abuse |
|
|
|
| 05 Dec 2011 07:30 PM |
Still same error.
## don't judge me ## |
|
|
| Report Abuse |
|
|
Cenort
|
  |
| Joined: 20 Nov 2009 |
| Total Posts: 9679 |
|
|
| 05 Dec 2011 07:32 PM |
C:\Menus\Menu for December.txt
Try typing that into your Windows Explorer and see if it works. Is there actually a file by that name? (Menu for December.txt) in the folder C:\Menus?? |
|
|
| Report Abuse |
|
|
Cenort
|
  |
| Joined: 20 Nov 2009 |
| Total Posts: 9679 |
|
|
| 05 Dec 2011 07:33 PM |
| Also, can you tell me what line 21 is? |
|
|
| Report Abuse |
|
|
|
| 05 Dec 2011 07:34 PM |
The program creates/is supposed to create both items itself.
## don't judge me ## |
|
|
| Report Abuse |
|
|