input a directory to Java to be proceed
up vote
-3
down vote
favorite
I have a problem in here. Whenever I entered the directory path that I copy from my computer to input a txt file to my program, it always said that the file is not found. Is there something wrong from my code?
System.out.println("insert directory file = ");
FileReader file = null;
try {
file = new FileReader(input.next());
BufferedReader readfile = new BufferedReader(file);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("n");
}
readfile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text = sb.toString();
//System.out.println(text);
System.out.println("Data entered");
System.out.println("Data length = "+text.length()+"n");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("File not found. Pease insert the proper file directory.n");
}
java filereader
New contributor
add a comment |
up vote
-3
down vote
favorite
I have a problem in here. Whenever I entered the directory path that I copy from my computer to input a txt file to my program, it always said that the file is not found. Is there something wrong from my code?
System.out.println("insert directory file = ");
FileReader file = null;
try {
file = new FileReader(input.next());
BufferedReader readfile = new BufferedReader(file);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("n");
}
readfile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text = sb.toString();
//System.out.println(text);
System.out.println("Data entered");
System.out.println("Data length = "+text.length()+"n");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("File not found. Pease insert the proper file directory.n");
}
java filereader
New contributor
Can you include the exception message output by adding e1.printStackTrace() to your catch. This will have information regarding the file name that cannot be found.
– billy.mccarthy
2 days ago
You should provide what path you paste to the console too. I guess there is a copy/paste error
– Hai Hoang
2 days ago
I write it like this D:\Home\data.txt but that wasn't work, is my code already correct?
– asdfghjkl
2 days ago
I already tried that e1.printStackTrace but that was making my program stop after I enter my pathd irectories
– asdfghjkl
2 days ago
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I have a problem in here. Whenever I entered the directory path that I copy from my computer to input a txt file to my program, it always said that the file is not found. Is there something wrong from my code?
System.out.println("insert directory file = ");
FileReader file = null;
try {
file = new FileReader(input.next());
BufferedReader readfile = new BufferedReader(file);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("n");
}
readfile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text = sb.toString();
//System.out.println(text);
System.out.println("Data entered");
System.out.println("Data length = "+text.length()+"n");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("File not found. Pease insert the proper file directory.n");
}
java filereader
New contributor
I have a problem in here. Whenever I entered the directory path that I copy from my computer to input a txt file to my program, it always said that the file is not found. Is there something wrong from my code?
System.out.println("insert directory file = ");
FileReader file = null;
try {
file = new FileReader(input.next());
BufferedReader readfile = new BufferedReader(file);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("n");
}
readfile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text = sb.toString();
//System.out.println(text);
System.out.println("Data entered");
System.out.println("Data length = "+text.length()+"n");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("File not found. Pease insert the proper file directory.n");
}
java filereader
java filereader
New contributor
New contributor
edited 2 days ago
Pang
6,756156399
6,756156399
New contributor
asked 2 days ago
asdfghjkl
31
31
New contributor
New contributor
Can you include the exception message output by adding e1.printStackTrace() to your catch. This will have information regarding the file name that cannot be found.
– billy.mccarthy
2 days ago
You should provide what path you paste to the console too. I guess there is a copy/paste error
– Hai Hoang
2 days ago
I write it like this D:\Home\data.txt but that wasn't work, is my code already correct?
– asdfghjkl
2 days ago
I already tried that e1.printStackTrace but that was making my program stop after I enter my pathd irectories
– asdfghjkl
2 days ago
add a comment |
Can you include the exception message output by adding e1.printStackTrace() to your catch. This will have information regarding the file name that cannot be found.
– billy.mccarthy
2 days ago
You should provide what path you paste to the console too. I guess there is a copy/paste error
– Hai Hoang
2 days ago
I write it like this D:\Home\data.txt but that wasn't work, is my code already correct?
– asdfghjkl
2 days ago
I already tried that e1.printStackTrace but that was making my program stop after I enter my pathd irectories
– asdfghjkl
2 days ago
Can you include the exception message output by adding e1.printStackTrace() to your catch. This will have information regarding the file name that cannot be found.
– billy.mccarthy
2 days ago
Can you include the exception message output by adding e1.printStackTrace() to your catch. This will have information regarding the file name that cannot be found.
– billy.mccarthy
2 days ago
You should provide what path you paste to the console too. I guess there is a copy/paste error
– Hai Hoang
2 days ago
You should provide what path you paste to the console too. I guess there is a copy/paste error
– Hai Hoang
2 days ago
I write it like this D:\Home\data.txt but that wasn't work, is my code already correct?
– asdfghjkl
2 days ago
I write it like this D:\Home\data.txt but that wasn't work, is my code already correct?
– asdfghjkl
2 days ago
I already tried that e1.printStackTrace but that was making my program stop after I enter my pathd irectories
– asdfghjkl
2 days ago
I already tried that e1.printStackTrace but that was making my program stop after I enter my pathd irectories
– asdfghjkl
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Your code segment run just fine in my laptop. So the problem maybe here :
file = new FileReader(input.next());
Did you use your Scanner for other input before read the path? Trying changing it to
String path = input.next();
file = new FileReader(path);
And print the path when error occur to see what actually was passed to your FileReader.
catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.n");
System.out.println("Your input path: " + path);
}
Here is the working code on my machine :
public static void main(String args) {
String path = null;
try (Scanner input = new Scanner(System.in)) {
System.out.print("Input your option = ");
int option = input.nextInt();
switch (option) {
case 1:
System.out.println("insert directory file = ");
String text = "";
path = input.next();
FileReader fileReader = new FileReader(path);
BufferedReader readfile = new BufferedReader(fileReader);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("n");
}
readfile.close();
} catch (IOException e) {
e.printStackTrace();
}
text = sb.toString();
System.out.println("Data entered");
System.out.println("Data length = " + text.length() + "n");
break;
default:
System.out.println("There is nothing to do.");
break;
}
} catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.");
System.out.println("Your input path is : " + path);
}
}
how did you enter the path directory? maybe I typed it wrong
– asdfghjkl
yesterday
Base on your code, I guess you read it from console by aScanner input;
. Is it right?
– Hai Hoang
yesterday
and yes, i did use scanner for my menu, case 1 is insert directory I use Scanner input = new Scanner(System.in) should I change it ?
– asdfghjkl
yesterday
Not necessary to change it. I just type the directoryD:\Home\data.txt
when the program ask me to. I think there is a high chance it is a typing error, so I suggest you print the path when error occur
– Hai Hoang
yesterday
wait I can't understand you. So basically my code was all okay? and here you ask me to print the path? print like what
– asdfghjkl
yesterday
|
show 6 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Your code segment run just fine in my laptop. So the problem maybe here :
file = new FileReader(input.next());
Did you use your Scanner for other input before read the path? Trying changing it to
String path = input.next();
file = new FileReader(path);
And print the path when error occur to see what actually was passed to your FileReader.
catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.n");
System.out.println("Your input path: " + path);
}
Here is the working code on my machine :
public static void main(String args) {
String path = null;
try (Scanner input = new Scanner(System.in)) {
System.out.print("Input your option = ");
int option = input.nextInt();
switch (option) {
case 1:
System.out.println("insert directory file = ");
String text = "";
path = input.next();
FileReader fileReader = new FileReader(path);
BufferedReader readfile = new BufferedReader(fileReader);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("n");
}
readfile.close();
} catch (IOException e) {
e.printStackTrace();
}
text = sb.toString();
System.out.println("Data entered");
System.out.println("Data length = " + text.length() + "n");
break;
default:
System.out.println("There is nothing to do.");
break;
}
} catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.");
System.out.println("Your input path is : " + path);
}
}
how did you enter the path directory? maybe I typed it wrong
– asdfghjkl
yesterday
Base on your code, I guess you read it from console by aScanner input;
. Is it right?
– Hai Hoang
yesterday
and yes, i did use scanner for my menu, case 1 is insert directory I use Scanner input = new Scanner(System.in) should I change it ?
– asdfghjkl
yesterday
Not necessary to change it. I just type the directoryD:\Home\data.txt
when the program ask me to. I think there is a high chance it is a typing error, so I suggest you print the path when error occur
– Hai Hoang
yesterday
wait I can't understand you. So basically my code was all okay? and here you ask me to print the path? print like what
– asdfghjkl
yesterday
|
show 6 more comments
up vote
0
down vote
accepted
Your code segment run just fine in my laptop. So the problem maybe here :
file = new FileReader(input.next());
Did you use your Scanner for other input before read the path? Trying changing it to
String path = input.next();
file = new FileReader(path);
And print the path when error occur to see what actually was passed to your FileReader.
catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.n");
System.out.println("Your input path: " + path);
}
Here is the working code on my machine :
public static void main(String args) {
String path = null;
try (Scanner input = new Scanner(System.in)) {
System.out.print("Input your option = ");
int option = input.nextInt();
switch (option) {
case 1:
System.out.println("insert directory file = ");
String text = "";
path = input.next();
FileReader fileReader = new FileReader(path);
BufferedReader readfile = new BufferedReader(fileReader);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("n");
}
readfile.close();
} catch (IOException e) {
e.printStackTrace();
}
text = sb.toString();
System.out.println("Data entered");
System.out.println("Data length = " + text.length() + "n");
break;
default:
System.out.println("There is nothing to do.");
break;
}
} catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.");
System.out.println("Your input path is : " + path);
}
}
how did you enter the path directory? maybe I typed it wrong
– asdfghjkl
yesterday
Base on your code, I guess you read it from console by aScanner input;
. Is it right?
– Hai Hoang
yesterday
and yes, i did use scanner for my menu, case 1 is insert directory I use Scanner input = new Scanner(System.in) should I change it ?
– asdfghjkl
yesterday
Not necessary to change it. I just type the directoryD:\Home\data.txt
when the program ask me to. I think there is a high chance it is a typing error, so I suggest you print the path when error occur
– Hai Hoang
yesterday
wait I can't understand you. So basically my code was all okay? and here you ask me to print the path? print like what
– asdfghjkl
yesterday
|
show 6 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Your code segment run just fine in my laptop. So the problem maybe here :
file = new FileReader(input.next());
Did you use your Scanner for other input before read the path? Trying changing it to
String path = input.next();
file = new FileReader(path);
And print the path when error occur to see what actually was passed to your FileReader.
catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.n");
System.out.println("Your input path: " + path);
}
Here is the working code on my machine :
public static void main(String args) {
String path = null;
try (Scanner input = new Scanner(System.in)) {
System.out.print("Input your option = ");
int option = input.nextInt();
switch (option) {
case 1:
System.out.println("insert directory file = ");
String text = "";
path = input.next();
FileReader fileReader = new FileReader(path);
BufferedReader readfile = new BufferedReader(fileReader);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("n");
}
readfile.close();
} catch (IOException e) {
e.printStackTrace();
}
text = sb.toString();
System.out.println("Data entered");
System.out.println("Data length = " + text.length() + "n");
break;
default:
System.out.println("There is nothing to do.");
break;
}
} catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.");
System.out.println("Your input path is : " + path);
}
}
Your code segment run just fine in my laptop. So the problem maybe here :
file = new FileReader(input.next());
Did you use your Scanner for other input before read the path? Trying changing it to
String path = input.next();
file = new FileReader(path);
And print the path when error occur to see what actually was passed to your FileReader.
catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.n");
System.out.println("Your input path: " + path);
}
Here is the working code on my machine :
public static void main(String args) {
String path = null;
try (Scanner input = new Scanner(System.in)) {
System.out.print("Input your option = ");
int option = input.nextInt();
switch (option) {
case 1:
System.out.println("insert directory file = ");
String text = "";
path = input.next();
FileReader fileReader = new FileReader(path);
BufferedReader readfile = new BufferedReader(fileReader);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("n");
}
readfile.close();
} catch (IOException e) {
e.printStackTrace();
}
text = sb.toString();
System.out.println("Data entered");
System.out.println("Data length = " + text.length() + "n");
break;
default:
System.out.println("There is nothing to do.");
break;
}
} catch (FileNotFoundException e1) {
System.out.println("File not found. Pease insert the proper file directory.");
System.out.println("Your input path is : " + path);
}
}
edited yesterday
answered yesterday
Hai Hoang
330313
330313
how did you enter the path directory? maybe I typed it wrong
– asdfghjkl
yesterday
Base on your code, I guess you read it from console by aScanner input;
. Is it right?
– Hai Hoang
yesterday
and yes, i did use scanner for my menu, case 1 is insert directory I use Scanner input = new Scanner(System.in) should I change it ?
– asdfghjkl
yesterday
Not necessary to change it. I just type the directoryD:\Home\data.txt
when the program ask me to. I think there is a high chance it is a typing error, so I suggest you print the path when error occur
– Hai Hoang
yesterday
wait I can't understand you. So basically my code was all okay? and here you ask me to print the path? print like what
– asdfghjkl
yesterday
|
show 6 more comments
how did you enter the path directory? maybe I typed it wrong
– asdfghjkl
yesterday
Base on your code, I guess you read it from console by aScanner input;
. Is it right?
– Hai Hoang
yesterday
and yes, i did use scanner for my menu, case 1 is insert directory I use Scanner input = new Scanner(System.in) should I change it ?
– asdfghjkl
yesterday
Not necessary to change it. I just type the directoryD:\Home\data.txt
when the program ask me to. I think there is a high chance it is a typing error, so I suggest you print the path when error occur
– Hai Hoang
yesterday
wait I can't understand you. So basically my code was all okay? and here you ask me to print the path? print like what
– asdfghjkl
yesterday
how did you enter the path directory? maybe I typed it wrong
– asdfghjkl
yesterday
how did you enter the path directory? maybe I typed it wrong
– asdfghjkl
yesterday
Base on your code, I guess you read it from console by a
Scanner input;
. Is it right?– Hai Hoang
yesterday
Base on your code, I guess you read it from console by a
Scanner input;
. Is it right?– Hai Hoang
yesterday
and yes, i did use scanner for my menu, case 1 is insert directory I use Scanner input = new Scanner(System.in) should I change it ?
– asdfghjkl
yesterday
and yes, i did use scanner for my menu, case 1 is insert directory I use Scanner input = new Scanner(System.in) should I change it ?
– asdfghjkl
yesterday
Not necessary to change it. I just type the directory
D:\Home\data.txt
when the program ask me to. I think there is a high chance it is a typing error, so I suggest you print the path when error occur– Hai Hoang
yesterday
Not necessary to change it. I just type the directory
D:\Home\data.txt
when the program ask me to. I think there is a high chance it is a typing error, so I suggest you print the path when error occur– Hai Hoang
yesterday
wait I can't understand you. So basically my code was all okay? and here you ask me to print the path? print like what
– asdfghjkl
yesterday
wait I can't understand you. So basically my code was all okay? and here you ask me to print the path? print like what
– asdfghjkl
yesterday
|
show 6 more comments
asdfghjkl is a new contributor. Be nice, and check out our Code of Conduct.
asdfghjkl is a new contributor. Be nice, and check out our Code of Conduct.
asdfghjkl is a new contributor. Be nice, and check out our Code of Conduct.
asdfghjkl is a new contributor. Be nice, and check out our Code of Conduct.
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200308%2finput-a-directory-to-java-to-be-proceed%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
Can you include the exception message output by adding e1.printStackTrace() to your catch. This will have information regarding the file name that cannot be found.
– billy.mccarthy
2 days ago
You should provide what path you paste to the console too. I guess there is a copy/paste error
– Hai Hoang
2 days ago
I write it like this D:\Home\data.txt but that wasn't work, is my code already correct?
– asdfghjkl
2 days ago
I already tried that e1.printStackTrace but that was making my program stop after I enter my pathd irectories
– asdfghjkl
2 days ago