Opening directories in Java
I would like to be able to open up directories using Java code, directory meaning a "folder" that contains a folder, which in turn contains files. This is the part of the code that I have now:
public void listFiles(String folder) {
File directory = new File(folder);
File contents = directory.listFiles();
System.out.println(contents);
For some reason, if I point the folder to the directory level, it returns this line:
[Ljava.io.File;@67d07b41
But if I point it one level down (at the folder level, which directly contains the files) then it will list out the file names in the folder just fine. Can someone give me pointers as to why this is not working for me?
java
|
show 2 more comments
I would like to be able to open up directories using Java code, directory meaning a "folder" that contains a folder, which in turn contains files. This is the part of the code that I have now:
public void listFiles(String folder) {
File directory = new File(folder);
File contents = directory.listFiles();
System.out.println(contents);
For some reason, if I point the folder to the directory level, it returns this line:
[Ljava.io.File;@67d07b41
But if I point it one level down (at the folder level, which directly contains the files) then it will list out the file names in the folder just fine. Can someone give me pointers as to why this is not working for me?
java
3
What do you mean sayingopen up
?
– Andrew Logvinov
Dec 17 '12 at 17:22
1
Can you post a runnable example that we can test? I don't understand the difference between folder level and directory level.
– sdasdadas
Dec 17 '12 at 17:24
new File(folder);
doesn't do anything except wrap the String with the folder's name. What are you expecting it to do?
– Peter Lawrey
Dec 17 '12 at 17:26
If by "open up", you mean list all of the contents, then all you need to do is declareFile contents = directory.listFiles();
– fireshadow52
Dec 17 '12 at 17:30
1
Have you tried using the Apache Commons FileUtils libraries? commons.apache.org/io/api-release/org/apache/commons/io/… I can't quite tell what you want to do, but I will bet that most of the major functionality has been done before and is included in Apache Commons.
– theJollySin
Dec 17 '12 at 18:06
|
show 2 more comments
I would like to be able to open up directories using Java code, directory meaning a "folder" that contains a folder, which in turn contains files. This is the part of the code that I have now:
public void listFiles(String folder) {
File directory = new File(folder);
File contents = directory.listFiles();
System.out.println(contents);
For some reason, if I point the folder to the directory level, it returns this line:
[Ljava.io.File;@67d07b41
But if I point it one level down (at the folder level, which directly contains the files) then it will list out the file names in the folder just fine. Can someone give me pointers as to why this is not working for me?
java
I would like to be able to open up directories using Java code, directory meaning a "folder" that contains a folder, which in turn contains files. This is the part of the code that I have now:
public void listFiles(String folder) {
File directory = new File(folder);
File contents = directory.listFiles();
System.out.println(contents);
For some reason, if I point the folder to the directory level, it returns this line:
[Ljava.io.File;@67d07b41
But if I point it one level down (at the folder level, which directly contains the files) then it will list out the file names in the folder just fine. Can someone give me pointers as to why this is not working for me?
java
java
edited Nov 23 '18 at 0:36
Mihai Chelaru
2,14291022
2,14291022
asked Dec 17 '12 at 17:20
user974047
69051839
69051839
3
What do you mean sayingopen up
?
– Andrew Logvinov
Dec 17 '12 at 17:22
1
Can you post a runnable example that we can test? I don't understand the difference between folder level and directory level.
– sdasdadas
Dec 17 '12 at 17:24
new File(folder);
doesn't do anything except wrap the String with the folder's name. What are you expecting it to do?
– Peter Lawrey
Dec 17 '12 at 17:26
If by "open up", you mean list all of the contents, then all you need to do is declareFile contents = directory.listFiles();
– fireshadow52
Dec 17 '12 at 17:30
1
Have you tried using the Apache Commons FileUtils libraries? commons.apache.org/io/api-release/org/apache/commons/io/… I can't quite tell what you want to do, but I will bet that most of the major functionality has been done before and is included in Apache Commons.
– theJollySin
Dec 17 '12 at 18:06
|
show 2 more comments
3
What do you mean sayingopen up
?
– Andrew Logvinov
Dec 17 '12 at 17:22
1
Can you post a runnable example that we can test? I don't understand the difference between folder level and directory level.
– sdasdadas
Dec 17 '12 at 17:24
new File(folder);
doesn't do anything except wrap the String with the folder's name. What are you expecting it to do?
– Peter Lawrey
Dec 17 '12 at 17:26
If by "open up", you mean list all of the contents, then all you need to do is declareFile contents = directory.listFiles();
– fireshadow52
Dec 17 '12 at 17:30
1
Have you tried using the Apache Commons FileUtils libraries? commons.apache.org/io/api-release/org/apache/commons/io/… I can't quite tell what you want to do, but I will bet that most of the major functionality has been done before and is included in Apache Commons.
– theJollySin
Dec 17 '12 at 18:06
3
3
What do you mean saying
open up
?– Andrew Logvinov
Dec 17 '12 at 17:22
What do you mean saying
open up
?– Andrew Logvinov
Dec 17 '12 at 17:22
1
1
Can you post a runnable example that we can test? I don't understand the difference between folder level and directory level.
– sdasdadas
Dec 17 '12 at 17:24
Can you post a runnable example that we can test? I don't understand the difference between folder level and directory level.
– sdasdadas
Dec 17 '12 at 17:24
new File(folder);
doesn't do anything except wrap the String with the folder's name. What are you expecting it to do?– Peter Lawrey
Dec 17 '12 at 17:26
new File(folder);
doesn't do anything except wrap the String with the folder's name. What are you expecting it to do?– Peter Lawrey
Dec 17 '12 at 17:26
If by "open up", you mean list all of the contents, then all you need to do is declare
File contents = directory.listFiles();
– fireshadow52
Dec 17 '12 at 17:30
If by "open up", you mean list all of the contents, then all you need to do is declare
File contents = directory.listFiles();
– fireshadow52
Dec 17 '12 at 17:30
1
1
Have you tried using the Apache Commons FileUtils libraries? commons.apache.org/io/api-release/org/apache/commons/io/… I can't quite tell what you want to do, but I will bet that most of the major functionality has been done before and is included in Apache Commons.
– theJollySin
Dec 17 '12 at 18:06
Have you tried using the Apache Commons FileUtils libraries? commons.apache.org/io/api-release/org/apache/commons/io/… I can't quite tell what you want to do, but I will bet that most of the major functionality has been done before and is included in Apache Commons.
– theJollySin
Dec 17 '12 at 18:06
|
show 2 more comments
1 Answer
1
active
oldest
votes
Change your code to:
public void listFiles(String folder){
File directory = new File(folder);
File contents = directory.listFiles();
for ( File f : contents) {
System.out.println(f.getAbsolutePath());
}
And you will see all full paths printed.
You're getting that weird output because you are printing an array object and that's what array objects will have as a toString()
. If you want to print the contents of the array you have to do it manually as above.
thanks! now i just need to loop through those folders to get the actual file names
– user974047
Dec 17 '12 at 18:27
add a comment |
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',
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%2f13918876%2fopening-directories-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Change your code to:
public void listFiles(String folder){
File directory = new File(folder);
File contents = directory.listFiles();
for ( File f : contents) {
System.out.println(f.getAbsolutePath());
}
And you will see all full paths printed.
You're getting that weird output because you are printing an array object and that's what array objects will have as a toString()
. If you want to print the contents of the array you have to do it manually as above.
thanks! now i just need to loop through those folders to get the actual file names
– user974047
Dec 17 '12 at 18:27
add a comment |
Change your code to:
public void listFiles(String folder){
File directory = new File(folder);
File contents = directory.listFiles();
for ( File f : contents) {
System.out.println(f.getAbsolutePath());
}
And you will see all full paths printed.
You're getting that weird output because you are printing an array object and that's what array objects will have as a toString()
. If you want to print the contents of the array you have to do it manually as above.
thanks! now i just need to loop through those folders to get the actual file names
– user974047
Dec 17 '12 at 18:27
add a comment |
Change your code to:
public void listFiles(String folder){
File directory = new File(folder);
File contents = directory.listFiles();
for ( File f : contents) {
System.out.println(f.getAbsolutePath());
}
And you will see all full paths printed.
You're getting that weird output because you are printing an array object and that's what array objects will have as a toString()
. If you want to print the contents of the array you have to do it manually as above.
Change your code to:
public void listFiles(String folder){
File directory = new File(folder);
File contents = directory.listFiles();
for ( File f : contents) {
System.out.println(f.getAbsolutePath());
}
And you will see all full paths printed.
You're getting that weird output because you are printing an array object and that's what array objects will have as a toString()
. If you want to print the contents of the array you have to do it manually as above.
answered Dec 17 '12 at 18:05
Maurício Linhares
32.4k13103139
32.4k13103139
thanks! now i just need to loop through those folders to get the actual file names
– user974047
Dec 17 '12 at 18:27
add a comment |
thanks! now i just need to loop through those folders to get the actual file names
– user974047
Dec 17 '12 at 18:27
thanks! now i just need to loop through those folders to get the actual file names
– user974047
Dec 17 '12 at 18:27
thanks! now i just need to loop through those folders to get the actual file names
– user974047
Dec 17 '12 at 18:27
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f13918876%2fopening-directories-in-java%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
3
What do you mean saying
open up
?– Andrew Logvinov
Dec 17 '12 at 17:22
1
Can you post a runnable example that we can test? I don't understand the difference between folder level and directory level.
– sdasdadas
Dec 17 '12 at 17:24
new File(folder);
doesn't do anything except wrap the String with the folder's name. What are you expecting it to do?– Peter Lawrey
Dec 17 '12 at 17:26
If by "open up", you mean list all of the contents, then all you need to do is declare
File contents = directory.listFiles();
– fireshadow52
Dec 17 '12 at 17:30
1
Have you tried using the Apache Commons FileUtils libraries? commons.apache.org/io/api-release/org/apache/commons/io/… I can't quite tell what you want to do, but I will bet that most of the major functionality has been done before and is included in Apache Commons.
– theJollySin
Dec 17 '12 at 18:06