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");
}









share|improve this question









New contributor




asdfghjkl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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















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");
}









share|improve this question









New contributor




asdfghjkl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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













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");
}









share|improve this question









New contributor




asdfghjkl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




asdfghjkl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




asdfghjkl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









Pang

6,756156399




6,756156399






New contributor




asdfghjkl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









asdfghjkl

31




31




New contributor




asdfghjkl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





asdfghjkl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






asdfghjkl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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










  • 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












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);
}
}


enter image description here






share|improve this answer























  • 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










  • 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










  • 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











Your Answer






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',
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
});


}
});






asdfghjkl is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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
































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);
}
}


enter image description here






share|improve this answer























  • 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










  • 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










  • 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















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);
}
}


enter image description here






share|improve this answer























  • 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










  • 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










  • 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













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);
}
}


enter image description here






share|improve this answer














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);
}
}


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








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 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










  • 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


















  • 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










  • 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










  • 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










asdfghjkl is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff