简体   繁体   English

Java在特定目录中创建文件

[英]Java creating a file in a certain directory

How do I get a json file I am creating to go into another directory. 如何获取要创建的json文件并进入另一个目录。 I am using a method that gives a file name and use this code to create it, its not the full code but Im pretty sure this is the only part I need to modify. 我正在使用一种提供文件名并使用此代码创建它的方法,它不是完整的代码,但是我很确定这是我需要修改的唯一部分。

FileInputStream in = new FileInputStream(fileName);
     JSONObject obj = new JSONObject(new JSONTokener(in));

right now it puts it in a directory but I want to put it in a folder in that directory. 现在,它将其放置在目录中,但我想将其放置在该目录中的文件夹中。

this also does not work 这也不起作用

FileInputStream in = new FileInputStream("/A/Ab/src/serv"+fileName);

/////////////////////////// ///////////////////////////

File cDir = new File("");
FileInputStream in = new FileInputStream(cDir.getAbsoluteFile() +fileName);

Try this: 尝试这个:

File dir = new File("/A/Ab/src/serv");
if (!dir.exists()) dir.mkdirs();

then: 然后:

FileInputStream in = new FileInputStream("/A/Ab/src/serv/" + filename);`enter code here`

If the directory already exists and you just want to create a new file, then simply do: 如果目录已经存在,而您只想创建一个新文件,则只需执行以下操作:

 FileInputStream in = new FileInputStream(new File("/A/Ab/src/serv/" + filename))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM