Reputation: 20068
File[] roots = File.listRoots();
for(File root: roots)
{
System.out.println(root);
}
I am new to java,i want to know how can i copy the result of this code in a String instead of print them. Thanks
Upvotes: 0
Views: 99
Reputation: 87533
Look at StringBuilder.
final StringBuilder sb = new StringBuilder();
final File[] roots = File.listRoots();
for(final File root: roots)
{
if (sb.length() > 0) sb.append("\n");
sb.append(root);
}
System.out.println(sb.toString()); // toString() not strictly necessary to println
Upvotes: 4
Reputation: 5853
StringBuilder string = new StringBuilder();
File[] roots = File.listRoots();
for(File root: roots)
{
string.append(root).append("\n");
}
System.out.println(string);
Upvotes: 2
Reputation: 8736
File[] roots = File.listRoots();
StringBuilder sb = new StringBuilder();
for(File root: roots)
{
sb.append(root.getName());
}
String s = sb.toString();
Upvotes: 0