1

I have a file called temperatures.txt saved to my Desktop, what code would I used to bring it up in Java..I am currently trying

File file = new File("C:/Windows/system32>/Desktop/temperatures.txt");

When I pull up the command on my schools computer it says C:\Windows\system32>

My slashes are backwards because when i compile it says "\" is an illegal character in BlueJ

3 Answers 3

1

Another complete solution :

import java.io.File; import java.io.IOException; public class Programme { public static void main(String[] args) { String yourDesktopPath = System.getProperty("user.home") + "\\Desktop\\"; try { File file = new File(yourDesktopPath + "temperatures.txt"); if (file.createNewFile()) { System.out.println("File is created!"); } else { System.out.println("File already exists."); } } catch (IOException e) { e.printStackTrace(); } } } 
Sign up to request clarification or add additional context in comments.

Comments

1

It's ok to use slashes instead of backslashes. You can use \\ for backslashes but that won't change anything.

I see 3 possible issues with the path.

1) The path contains the character > which doesn't seem like it belongs there. It should probably be:

File file = new File("C:/Windows/system32/Desktop/temperatures.txt"); 

2) windows\system32 is a system directory and it could be that windows restricts access to that folder.

3) That is not the common desktop directory. Usually the desktop is in the user directory. For example here:

C:\Users\YourName\Desktop 

Comments

1

/ is a forward slash. It is the path separator in Unix-based operating systems.

\ is a backslash. It is the path separator in Windows. Both should work, though. Java will translate approprately.

\ in Java string literals (and similarly in most other programming languages) is an escape character. When you have a string literal written as "C:\Windows..." Your IDE is complaining because Java is trying to treat "\W" as an escape sequence.

To type a backslash character in a string literal, you need to escape the backslash using another backslash. So, replace \ with \\.

File file = new File("C:\\Windows\\system32>\\Desktop\\temperatures.txt"); 

1 Comment

I still got: java.io.FileNotFoundException: C:\Windows\system32\Desktop\temperatures.txt (The system cannot find the path specified)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.