Adrian
Adrian

Reputation: 20068

Help with java File

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

Answers (4)

Bert F
Bert F

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

Mauricio
Mauricio

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

McGlone
McGlone

Reputation: 3444

String s = Arrays.toString(roots);

Upvotes: 0

Navi
Navi

Reputation: 8736

  File[] roots = File.listRoots();
  StringBuilder sb = new StringBuilder();

  for(File root: roots)  
  {  
    sb.append(root.getName());  
  }

  String s = sb.toString();

Upvotes: 0

Related Questions