开发者

What variable should I put into this declaration to make it work?

So I have this code that pops up a file chooser and reads the file:

JFileChooser chooser = new JFileChooser(); 
File file = null; 
int returnValue = chooser.showOpenDialog( null ) ; 
if( returnValue == JFileChooser.APPROVE_OPTION ) { 
   file = chooser.getSelectedFile() ;
} 
if(file != null) { 
   String filePath = file.getPath();
} 

// String filePath (that's what i'm trying to input) = "Users/Bill/Desktop/hello.txt";
try { 
   ReadFile files = new ReadFile(***)开发者_高级运维;
   String[] lines = files.OpenFile();

the three asterisks (*) represent the class path of the file to be read. What should I put there if I want to read the file? Before this, I hard-coded the class-path and passed it in and it worked, but now, the class-path can be whatever the user chooses. Thanks for your help!


I don't know what your ReadFile thing is, but it looks like it takes a string representing the file path. In which case, you probably want to give it file.getPath().


Try using this instead of your code in try block:

FileReader fr = new FileReader(filePath);
FileWriter outputStream = new FileWriter("filename.txt");;
int c;

while ((c = fr.read()) != -1) {
    outputStream.write(c);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜