开发者

how to open a file with name in a string in java

 FileInputStream fis = new FileInputStream("./abc.txt");

I wa开发者_Go百科nt to replace the quoted value by a string

I also want to fill the string with different filenames in a switch statement so that I can open different files and perform repeated operations on the files data.

how do I go about this?


FileInputStream fis = new FileInputStream(filename);

Note that there is no String.valueOf(String). If there was, it would be pointless as it would likely just return the String you passed it.


Note that Java doesn't support Strings as case parameters (or at least you'll have to wait for Java 7 to do so), so you'll need a set of if/else comparisons to do different things for different filenames.

if ("a.txt".equals(filename)) {
  // do something
}
if ("b.txt".equals(filename)) {
  // do something
}

etc.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜