Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何重用文件处理程序打开多个文件_File - Fatal编程技术网

File 如何重用文件处理程序打开多个文件

File 如何重用文件处理程序打开多个文件,file,File,我必须编写一段无法预先确定要创建的文件名的代码 因此,我想保留一个文件处理程序。关上这个。使用相同的文件处理程序创建另一个文件 示例代码: 现在,我将使用不同的文件名多次调用这个函数示例 当我尝试上面的示例代码时,我得到了NullPointerException。谁能告诉我哪里出了问题 问候,, Harish您在哪一行获得NPE?请出示错误日志好吗? public void sample(String input) { // The value of this string ch

我必须编写一段无法预先确定要创建的文件名的代码

因此,我想保留一个文件处理程序。关上这个。使用相同的文件处理程序创建另一个文件

示例代码: 现在,我将使用不同的文件名多次调用这个函数示例

当我尝试上面的示例代码时,我得到了NullPointerException。谁能告诉我哪里出了问题

问候,,
Harish

您在哪一行获得NPE?请出示错误日志好吗?
public void sample(String input)  
{  
    // The value of this string changes dynamically                                            
    // and accordingly a new file with that needs to be created.

    String fileName = input; 
    File file = new File(fileName);
    FileWriter fw = new FileWriter(file);
    BufferedWriter bw= new BufferedWriter(fw);

    // code to write contents to that file.
    bw.close();
}