Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

java - copy Path in file writer Class into File Class

i would like to create a New file using filewriter Class and use the Exact path in file class(dont want to copy/paste the manually, any chnages in file writer path , we would like to reflect the instances) to get absolute path of the particular file .Can anyone suggest me how.

First i would to create a filewriter and later want to use File to display its attributes such that any chnages in Fiewriter URL would be displayed.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class files_read {

 public static void main(String[] args) {
     // TODO Auto-generated method stub

try {
FileWriter FW=new FileWriter("C:/Users/91870/Downloads/Boss.txt");

FW.write("yoyo");
FW.close();
File f=new File(FW);

}
catch(IOException ie)

{
 System.out.println("An error occurred.");
 ie.printStackTrace();
}

 }
}````
question from:https://stackoverflow.com/questions/65858749/copy-path-in-file-writer-class-into-file-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should do it the other way around. First create the File instance and then the FileWriter with that File instance.

File f = new File("C:/Users/91870/Downloads/Boss.txt");
FileWriter fw = new FileWriter(f);
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...