There is an error in my code. Can you show me how to get rid of the Files.write
ID: 3752887 • Letter: T
Question
There is an error in my code. Can you show me how to get rid of the Files.write error and can you write a code in the main method that will run the for and if statement. It has to be in the main method. I did not provide what listofnames.java is but it should not matter. I want the parameters to stay the exact same for Files.write.
package finder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
public class location {
String find = "listofnames.java";
Path pathOfFile = Paths.get(find);
public static void main(String[] args) {
}
private location (String [] args) {
try {
List<String> linesInFile = Files.readAllLines(pathOfFile,Charset.defaultCharset());
for (String i : linesInFile){
if (args.length > 0) {
find = args[0];
System.out.println(i);
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
void writeToFile( Path toSearch, List<String> toWrite ) {
List <String> toOuttext = new ArrayList <String> ();
toOuttext.add ("Printing data");
try {
Files.write(find+".data", toOuttext,Charset.defaultCharset());
}
catch (IOException e) {
System.out.println(e);
}
}
}
Explanation / Answer
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
public class location {
private static String find = "listofnames.java";
private static Path pathOfFile = Paths.get(find);
public static void main(String[] args) {
if (args.length > 0) {
find = args[0];
pathOfFile = Paths.get(find);
}
try {
List<String> linesInFile = Files.readAllLines(pathOfFile,Charset.defaultCharset());
for (String i : linesInFile){
System.out.println(i);
}
}
catch (IOException e) {
System.out.println(e);
}
}
static void writeToFile( Path toSearch, List<String> toWrite ) {
//List <String> toOuttext = new ArrayList <String> ();
//toOuttext.add ("Printing data");
try {
Files.write(toSearch, toWrite,Charset.defaultCharset());
}
catch (IOException e) {
System.out.println(e);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.