d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > What Did I Do Wrong?
Add Reply New Topic New Poll
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Nov 26 2019 04:01pm
Code
package com.company;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

public static void main(String[] args) {

Path path = FileSystems.getDefault().getPath("WorkingDirectoryFile.txt");
printFile(path);

Path filePath = FileSystems.getDefault().getPath("files", "SubdirectoryFile.txt");
printFile(filePath);

filePath = Paths.get("C:\\Users\\ferfy\\Desktop\\Java Code\\Course\\Section 14-BasicInput&Output\\OutThere.txt");
printFile(filePath);



}



private static void printFile(Path path) {
try(BufferedReader fileReader = Files.newBufferedReader(path)) {
String line;
while((line = fileReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}




}




Code
"C:\Program Files\Java\jdk-11.0.3\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.3\lib\idea_rt.jar=49481:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\user\Desktop\Java Code\Course\Section 14-BasicInput&Output\Paths\out\production\Paths" com.company.Main
File within the working directory.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Ut suscipit eu lectus non volutpat.
In consequat ac lacus vitae ornare.
Vestibulum cursus urna tellus, vitae placerat elit euismod in.
File within subdirectory.
In consequat ac lacus vitae ornare.
Vestibulum cursus urna tellus, vitae placerat elit euismod in.
Aliquam at lobortis lacus.
C:\Users\user\Desktop\Java Code\Course\Section 14-BasicInput&Output\OutThere.txt
java.nio.file.NoSuchFileException: C:\Users\user\Desktop\Java Code\Course\Section 14-BasicInput&Output\OutThere.txt
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:231)
at java.base/java.nio.file.Files.newByteChannel(Files.java:370)
at java.base/java.nio.file.Files.newByteChannel(Files.java:421)
at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:420)
at java.base/java.nio.file.Files.newInputStream(Files.java:155)
at java.base/java.nio.file.Files.newBufferedReader(Files.java:2838)
at java.base/java.nio.file.Files.newBufferedReader(Files.java:2870)
at com.company.Main.printFile(Main.java:30)
at com.company.Main.main(Main.java:21)








All of them work except for this:
Code
filePath = Paths.get("C:\\Users\\user\\Desktop\\Java Code\\Course\\Section 14-BasicInput&Output\\OutThere.txt");
printFile(filePath);





Also, this PC is windows 10 OS.

Not sure what i did wrong. the path is correct......

Am i typing the path wrong? or something? idk

I copied/pasted path from windows explorer so idk
Member
Posts: 2,619
Joined: May 21 2004
Gold: 21,934.00
Nov 26 2019 05:07pm
Paths.get() shouldn't need the ass backwards windows notation to work.

try this instead:

Code
filePath = Paths.get("C:/Users/ferfy/Desktop/Java Code/Course/Section 14-BasicInput&Output/OutThere.txt");


Additionally, I assume you just typed 'user' in the bottom snippet and meant 'ferfy'? I doubt you have a user named 'user' on a windows machine.

Upon further examination, is the 'ferfy' part wrong? Your logs show C:\Users\user as the dir. What exactly is your user account folder name on windows?

This post was edited by frixionburne on Nov 26 2019 05:12pm
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Nov 26 2019 05:22pm
Quote (frixionburne @ Nov 26 2019 07:07pm)
Paths.get() shouldn't need the ass backwards windows notation to work.

try this instead:

Code
filePath = Paths.get("C:/Users/ferfy/Desktop/Java Code/Course/Section 14-BasicInput&Output/OutThere.txt");


Additionally, I assume you just typed 'user' in the bottom snippet and meant 'ferfy'? I doubt you have a user named 'user' on a windows machine.

Upon further examination, is the 'ferfy' part wrong? Your logs show C:\Users\user as the dir. What exactly is your user account folder name on windows?


tried what you put, but get the same errors


Code

"C:\Program Files\Java\jdk-11.0.3\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.3\lib\idea_rt.jar=53917:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\ferfy\Desktop\Java Code\Course\Section 14-BasicInput&Output\Paths\out\production\Paths" com.company.Main
File within the working directory.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Ut suscipit eu lectus non volutpat.
In consequat ac lacus vitae ornare.
Vestibulum cursus urna tellus, vitae placerat elit euismod in.
File within subdirectory.
In consequat ac lacus vitae ornare.
Vestibulum cursus urna tellus, vitae placerat elit euismod in.
Aliquam at lobortis lacus.
C:\Users\ferfy\Desktop\Java Code\Course\Section 14-BasicInput&Output\OutThere.txt
java.nio.file.NoSuchFileException: C:\Users\ferfy\Desktop\Java Code\Course\Section 14-BasicInput&Output\OutThere.txt
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:231)
at java.base/java.nio.file.Files.newByteChannel(Files.java:370)
at java.base/java.nio.file.Files.newByteChannel(Files.java:421)
at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:420)
at java.base/java.nio.file.Files.newInputStream(Files.java:155)
at java.base/java.nio.file.Files.newBufferedReader(Files.java:2838)
at java.base/java.nio.file.Files.newBufferedReader(Files.java:2870)
at com.company.Main.printFile(Main.java:30)
at com.company.Main.main(Main.java:21)


This post was edited by ferf on Nov 26 2019 05:23pm
Member
Posts: 2,619
Joined: May 21 2004
Gold: 21,934.00
Nov 26 2019 08:16pm
open a cmd prompt and cd to 'C:\Users\ferfy\Desktop\Java Code\Course\Section 14-BasicInput&Output\' then paste the output of the 'dir' command
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Nov 26 2019 09:12pm
Quote (frixionburne @ Nov 26 2019 10:16pm)
open a cmd prompt and cd to 'C:\Users\ferfy\Desktop\Java Code\Course\Section 14-BasicInput&Output\' then paste the output of the 'dir' command


had to rename directory for cd to work properly

here's the cmd output from dir command in directory:


https://imgur.com/HMzLbDo




oh also: this is how it's named now:

C:\Users\ferfy\Desktop\Java Code\Course\Section 14-BasicInputOutput




also still getting the same NIO exception nosuchfileexception with renamed directory



Also, i'm not running intellij as admin.... this is on a limited account, but i don't see how that should matter, as the files were trying to access are the limited accounts files...

This post was edited by ferf on Nov 26 2019 09:38pm
Member
Posts: 2,619
Joined: May 21 2004
Gold: 21,934.00
Nov 26 2019 10:04pm
You didn't need to rename everything, and if you just renamed a directory to make cd work, I'm not sure if I can actually help you here. Something is wrong with trying to find that file, be it the path you're asking it to find or whatever your dev env looks like. I just wanted the dir output to see what everything is called, and to see if those files existed.

Generally, you don't assume someone is going to rename a directory in order to change to it... I'm kind of thunderstruck right now.

This post was edited by frixionburne on Nov 26 2019 10:05pm
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Nov 26 2019 10:08pm
Quote (frixionburne @ Nov 27 2019 12:04am)
You didn't need to rename everything, and if you just renamed a directory to make cd work, I'm not sure if I can actually help you here. Something is wrong with trying to find that file, be it the path you're asking it to find or whatever your dev env looks like. I just wanted the dir output to see what everything is called, and to see if those files existed.

Generally, you don't assume someone is going to rename a directory in order to change to it... I'm kind of thunderstruck right now.




Not sure what you mean? I renamed the directory cuz cd wasn't working with the '&' in the name
i did post the output of dir command in the screenshot...
Member
Posts: 2,619
Joined: May 21 2004
Gold: 21,934.00
Nov 26 2019 10:18pm
Quote (ferf @ Nov 27 2019 12:08am)
Not sure what you mean? I renamed the directory cuz cd wasn't working with the '&' in the name
i did post the output of dir command in the screenshot...


Ahh I see what you did. you might have needed to delimit the ampersand because windows, I don't know. I dev on linux vms.

You shouldn't need to run idea in admin.

Did you change the Paths.get() line in your code to point to 'C:\Users\ferfy\Desktop\Java Code\Course\Section 14-BasicInputOutput\OutThere.txt.txt'?

I think your whole problem is because the file is for some reason named OutThere.txt.txt. You have two file suffixes for some reason.

This post was edited by frixionburne on Nov 26 2019 10:19pm
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Nov 26 2019 10:21pm
Quote (frixionburne @ Nov 27 2019 12:18am)
Ahh I see what you did. you might have needed to delimit the ampersand because windows, I don't know. I dev on linux vms.

You shouldn't need to run idea in admin.

Did you change the Paths.get() line in your code to point to 'C:\Users\ferfy\Desktop\Java Code\Course\Section 14-BasicInputOutput\OutThere.txt.txt'?

I think your whole problem is because the file is for some reason named OutThere.txt.txt. You have two file suffixes for some reason.



u were right it was cuz of txt.txt

ty its workign now :)
Member
Posts: 2,619
Joined: May 21 2004
Gold: 21,934.00
Nov 26 2019 10:54pm
Excellent, good to hear!
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll