No such file or directory found even though the file is in the same package The Next CEO of Stack OverflowJava - Getting file from same packageIf the file I am trying to read is in the same package as the java file I'm working with, why am I still getting a “FileNotFoundException”?Compile all files in src?How to load file to ArrayList from src folderHow can i copy error from console to file and where to include the code and what will be the code?Filenotfound return catchWhile expected ErrorAlternative Methods in codenameoneWould it make any difference giving arguments using scanner class instead of command line arguments?Console says scanner is closed , I need it to reopen. Java error
Another proof that dividing by 0 does not exist -- is it right?
What did the word "leisure" mean in late 18th Century usage?
How does a dynamic QR code work?
How to show a landlord what we have in savings?
Could a dragon use its wings to swim?
Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico
Calculate the Mean mean of two numbers
How to pronounce fünf in 45
Is it possible to create a QR code using text?
Is it correct to say moon starry nights?
Can you teleport closer to a creature you are Frightened of?
Is it possible to make a 9x9 table fit within the default margins?
Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact
Is a linearly independent set whose span is dense a Schauder basis?
How to compactly explain secondary and tertiary characters without resorting to stereotypes?
How seriously should I take size and weight limits of hand luggage?
Salesforce opportunity stages
pgfplots: How to draw a tangent graph below two others?
How to coordinate airplane tickets?
Is this a new Fibonacci Identity?
Mathematica command that allows it to read my intentions
How can I prove that a state of equilibrium is unstable?
Does Germany produce more waste than the US?
Free fall ellipse or parabola?
No such file or directory found even though the file is in the same package
The Next CEO of Stack OverflowJava - Getting file from same packageIf the file I am trying to read is in the same package as the java file I'm working with, why am I still getting a “FileNotFoundException”?Compile all files in src?How to load file to ArrayList from src folderHow can i copy error from console to file and where to include the code and what will be the code?Filenotfound return catchWhile expected ErrorAlternative Methods in codenameoneWould it make any difference giving arguments using scanner class instead of command line arguments?Console says scanner is closed , I need it to reopen. Java error
I am trying to scan the "loremIpsum.txt"
file to a String using the split method of the class String to store each word in a different position of an array, and last use a HashSet to find if there is any word repetition in the text.
But Eclipse doesn't recognize the file even though it is in the same package. I was wondering if there is something wrong with my code?
package Lab5;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
public class Lorem
public static void main(String[] args)
String[] loremIpsum = null;
try
loremIpsum = new Scanner(new File("loremIpsum.txt")).next().split(" ");
catch (FileNotFoundException e)
e.printStackTrace();
System.out.println(loremIpsum.length);
HashSet h = new HashSet();
for(int i=0;i<loremIpsum.length;i++)
String word=loremIpsum[i];
System.out.println(word);
if(h.contains(word))
System.out.println("we found a duplicate");
else
h.add(word);
Error message and proof "lorem.txt" is in the same package:
java split java.util.scanner hashset lorem-ipsum
add a comment |
I am trying to scan the "loremIpsum.txt"
file to a String using the split method of the class String to store each word in a different position of an array, and last use a HashSet to find if there is any word repetition in the text.
But Eclipse doesn't recognize the file even though it is in the same package. I was wondering if there is something wrong with my code?
package Lab5;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
public class Lorem
public static void main(String[] args)
String[] loremIpsum = null;
try
loremIpsum = new Scanner(new File("loremIpsum.txt")).next().split(" ");
catch (FileNotFoundException e)
e.printStackTrace();
System.out.println(loremIpsum.length);
HashSet h = new HashSet();
for(int i=0;i<loremIpsum.length;i++)
String word=loremIpsum[i];
System.out.println(word);
if(h.contains(word))
System.out.println("we found a duplicate");
else
h.add(word);
Error message and proof "lorem.txt" is in the same package:
java split java.util.scanner hashset lorem-ipsum
Side note:Scanner#next
uses whitespace as the default delimiter, so callingsplit(" ")
will result in a String array of size 1 or possibly 0. You should useScanner#nextLine
withsplit(" ")
.
– Jonny Henly
Mar 7 at 19:50
May be an XY problem rather than a duplicate. For a beginning Java exercise, it seems like the problem may lie more with the presumption that the text file is in the same directory as the code, rather than how to accomplish that.
– Andy Thomas
Mar 7 at 19:58
add a comment |
I am trying to scan the "loremIpsum.txt"
file to a String using the split method of the class String to store each word in a different position of an array, and last use a HashSet to find if there is any word repetition in the text.
But Eclipse doesn't recognize the file even though it is in the same package. I was wondering if there is something wrong with my code?
package Lab5;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
public class Lorem
public static void main(String[] args)
String[] loremIpsum = null;
try
loremIpsum = new Scanner(new File("loremIpsum.txt")).next().split(" ");
catch (FileNotFoundException e)
e.printStackTrace();
System.out.println(loremIpsum.length);
HashSet h = new HashSet();
for(int i=0;i<loremIpsum.length;i++)
String word=loremIpsum[i];
System.out.println(word);
if(h.contains(word))
System.out.println("we found a duplicate");
else
h.add(word);
Error message and proof "lorem.txt" is in the same package:
java split java.util.scanner hashset lorem-ipsum
I am trying to scan the "loremIpsum.txt"
file to a String using the split method of the class String to store each word in a different position of an array, and last use a HashSet to find if there is any word repetition in the text.
But Eclipse doesn't recognize the file even though it is in the same package. I was wondering if there is something wrong with my code?
package Lab5;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
public class Lorem
public static void main(String[] args)
String[] loremIpsum = null;
try
loremIpsum = new Scanner(new File("loremIpsum.txt")).next().split(" ");
catch (FileNotFoundException e)
e.printStackTrace();
System.out.println(loremIpsum.length);
HashSet h = new HashSet();
for(int i=0;i<loremIpsum.length;i++)
String word=loremIpsum[i];
System.out.println(word);
if(h.contains(word))
System.out.println("we found a duplicate");
else
h.add(word);
Error message and proof "lorem.txt" is in the same package:
java split java.util.scanner hashset lorem-ipsum
java split java.util.scanner hashset lorem-ipsum
edited Mar 7 at 20:20
stevie lol
asked Mar 7 at 19:08
stevie lolstevie lol
112
112
Side note:Scanner#next
uses whitespace as the default delimiter, so callingsplit(" ")
will result in a String array of size 1 or possibly 0. You should useScanner#nextLine
withsplit(" ")
.
– Jonny Henly
Mar 7 at 19:50
May be an XY problem rather than a duplicate. For a beginning Java exercise, it seems like the problem may lie more with the presumption that the text file is in the same directory as the code, rather than how to accomplish that.
– Andy Thomas
Mar 7 at 19:58
add a comment |
Side note:Scanner#next
uses whitespace as the default delimiter, so callingsplit(" ")
will result in a String array of size 1 or possibly 0. You should useScanner#nextLine
withsplit(" ")
.
– Jonny Henly
Mar 7 at 19:50
May be an XY problem rather than a duplicate. For a beginning Java exercise, it seems like the problem may lie more with the presumption that the text file is in the same directory as the code, rather than how to accomplish that.
– Andy Thomas
Mar 7 at 19:58
Side note:
Scanner#next
uses whitespace as the default delimiter, so calling split(" ")
will result in a String array of size 1 or possibly 0. You should use Scanner#nextLine
with split(" ")
.– Jonny Henly
Mar 7 at 19:50
Side note:
Scanner#next
uses whitespace as the default delimiter, so calling split(" ")
will result in a String array of size 1 or possibly 0. You should use Scanner#nextLine
with split(" ")
.– Jonny Henly
Mar 7 at 19:50
May be an XY problem rather than a duplicate. For a beginning Java exercise, it seems like the problem may lie more with the presumption that the text file is in the same directory as the code, rather than how to accomplish that.
– Andy Thomas
Mar 7 at 19:58
May be an XY problem rather than a duplicate. For a beginning Java exercise, it seems like the problem may lie more with the presumption that the text file is in the same directory as the code, rather than how to accomplish that.
– Andy Thomas
Mar 7 at 19:58
add a comment |
3 Answers
3
active
oldest
votes
The file will be looked for in the project directory (where bin
and src
folders are located). Move the file there.
add a comment |
You need to pass the parameter as a path.
try this
String path = new File("").getAbsolutePath();
path.concat("/loremIpsum.txt");
loremIpsum = new Scanner(new File(path)).next().split(" ");
basically youre just finding the current path and appending the file name youre wanting to read from.
Like the others said though, you can move it to your working directory as well.
Cheers!
The first two statements seem to be a rather roundabout way of accomplishing the same task asnew File("loremIpsum.txt")
.
– Andy Thomas
Mar 7 at 20:12
add a comment |
When you call the File constructor with a relative path, it's relative to the working directory.
That usually won't be the same directory as the code calling the constructor. But that's okay, because if your file can be specified when you run the application, you don't want to presume that anyway.
You can specify the working directory in the Eclipse run configuration, on the Arguments tab.
You can see how a relative path has been resolved using the File method getAbsolutePath().
try {
File myFile = new File("loremIpsum.txt");
System.out.println("Absolute path = " + myFile.getAbsolutePath() );
loremIpsum = new Scanner(myFile).next().split(" ");
...
add a comment |
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55051148%2fno-such-file-or-directory-found-even-though-the-file-is-in-the-same-package%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The file will be looked for in the project directory (where bin
and src
folders are located). Move the file there.
add a comment |
The file will be looked for in the project directory (where bin
and src
folders are located). Move the file there.
add a comment |
The file will be looked for in the project directory (where bin
and src
folders are located). Move the file there.
The file will be looked for in the project directory (where bin
and src
folders are located). Move the file there.
answered Mar 7 at 19:40
OuroborosOuroboros
112
112
add a comment |
add a comment |
You need to pass the parameter as a path.
try this
String path = new File("").getAbsolutePath();
path.concat("/loremIpsum.txt");
loremIpsum = new Scanner(new File(path)).next().split(" ");
basically youre just finding the current path and appending the file name youre wanting to read from.
Like the others said though, you can move it to your working directory as well.
Cheers!
The first two statements seem to be a rather roundabout way of accomplishing the same task asnew File("loremIpsum.txt")
.
– Andy Thomas
Mar 7 at 20:12
add a comment |
You need to pass the parameter as a path.
try this
String path = new File("").getAbsolutePath();
path.concat("/loremIpsum.txt");
loremIpsum = new Scanner(new File(path)).next().split(" ");
basically youre just finding the current path and appending the file name youre wanting to read from.
Like the others said though, you can move it to your working directory as well.
Cheers!
The first two statements seem to be a rather roundabout way of accomplishing the same task asnew File("loremIpsum.txt")
.
– Andy Thomas
Mar 7 at 20:12
add a comment |
You need to pass the parameter as a path.
try this
String path = new File("").getAbsolutePath();
path.concat("/loremIpsum.txt");
loremIpsum = new Scanner(new File(path)).next().split(" ");
basically youre just finding the current path and appending the file name youre wanting to read from.
Like the others said though, you can move it to your working directory as well.
Cheers!
You need to pass the parameter as a path.
try this
String path = new File("").getAbsolutePath();
path.concat("/loremIpsum.txt");
loremIpsum = new Scanner(new File(path)).next().split(" ");
basically youre just finding the current path and appending the file name youre wanting to read from.
Like the others said though, you can move it to your working directory as well.
Cheers!
answered Mar 7 at 19:43
Jandrei PittiJandrei Pitti
2617
2617
The first two statements seem to be a rather roundabout way of accomplishing the same task asnew File("loremIpsum.txt")
.
– Andy Thomas
Mar 7 at 20:12
add a comment |
The first two statements seem to be a rather roundabout way of accomplishing the same task asnew File("loremIpsum.txt")
.
– Andy Thomas
Mar 7 at 20:12
The first two statements seem to be a rather roundabout way of accomplishing the same task as
new File("loremIpsum.txt")
.– Andy Thomas
Mar 7 at 20:12
The first two statements seem to be a rather roundabout way of accomplishing the same task as
new File("loremIpsum.txt")
.– Andy Thomas
Mar 7 at 20:12
add a comment |
When you call the File constructor with a relative path, it's relative to the working directory.
That usually won't be the same directory as the code calling the constructor. But that's okay, because if your file can be specified when you run the application, you don't want to presume that anyway.
You can specify the working directory in the Eclipse run configuration, on the Arguments tab.
You can see how a relative path has been resolved using the File method getAbsolutePath().
try {
File myFile = new File("loremIpsum.txt");
System.out.println("Absolute path = " + myFile.getAbsolutePath() );
loremIpsum = new Scanner(myFile).next().split(" ");
...
add a comment |
When you call the File constructor with a relative path, it's relative to the working directory.
That usually won't be the same directory as the code calling the constructor. But that's okay, because if your file can be specified when you run the application, you don't want to presume that anyway.
You can specify the working directory in the Eclipse run configuration, on the Arguments tab.
You can see how a relative path has been resolved using the File method getAbsolutePath().
try {
File myFile = new File("loremIpsum.txt");
System.out.println("Absolute path = " + myFile.getAbsolutePath() );
loremIpsum = new Scanner(myFile).next().split(" ");
...
add a comment |
When you call the File constructor with a relative path, it's relative to the working directory.
That usually won't be the same directory as the code calling the constructor. But that's okay, because if your file can be specified when you run the application, you don't want to presume that anyway.
You can specify the working directory in the Eclipse run configuration, on the Arguments tab.
You can see how a relative path has been resolved using the File method getAbsolutePath().
try {
File myFile = new File("loremIpsum.txt");
System.out.println("Absolute path = " + myFile.getAbsolutePath() );
loremIpsum = new Scanner(myFile).next().split(" ");
...
When you call the File constructor with a relative path, it's relative to the working directory.
That usually won't be the same directory as the code calling the constructor. But that's okay, because if your file can be specified when you run the application, you don't want to presume that anyway.
You can specify the working directory in the Eclipse run configuration, on the Arguments tab.
You can see how a relative path has been resolved using the File method getAbsolutePath().
try {
File myFile = new File("loremIpsum.txt");
System.out.println("Absolute path = " + myFile.getAbsolutePath() );
loremIpsum = new Scanner(myFile).next().split(" ");
...
edited Mar 7 at 19:53
answered Mar 7 at 19:41
Andy ThomasAndy Thomas
68.4k980134
68.4k980134
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55051148%2fno-such-file-or-directory-found-even-though-the-file-is-in-the-same-package%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Side note:
Scanner#next
uses whitespace as the default delimiter, so callingsplit(" ")
will result in a String array of size 1 or possibly 0. You should useScanner#nextLine
withsplit(" ")
.– Jonny Henly
Mar 7 at 19:50
May be an XY problem rather than a duplicate. For a beginning Java exercise, it seems like the problem may lie more with the presumption that the text file is in the same directory as the code, rather than how to accomplish that.
– Andy Thomas
Mar 7 at 19:58