Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.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
对于以null结尾的字符,此代码的Java适当I/O_Java_Linux_File Io_Io - Fatal编程技术网

对于以null结尾的字符,此代码的Java适当I/O

对于以null结尾的字符,此代码的Java适当I/O,java,linux,file-io,io,Java,Linux,File Io,Io,这段代码在Java中的等价物是什么?我已经把它的一部分放进去了,我对I/O部分很感兴趣: int fd = open(FILE_NAME, O_WRONLY); int ret = 0; if (fd == -1) { exit(EXIT_FAILURE); } while (1) { ret = write(fd, "\0", 1); } 更新: 代码不复制文件。它只在文件中每隔X秒写入一个字节(?)/字符(?(不确定是什么) F

这段代码在
Java
中的等价物是什么?我已经把它的一部分放进去了,我对I/O部分很感兴趣:

int fd = open(FILE_NAME, O_WRONLY);  
int ret = 0;  
if (fd == -1) {         
    exit(EXIT_FAILURE);  
}  
while (1) {  
    ret = write(fd, "\0", 1);  
}  
更新:
代码不复制文件。它只在文件中每隔X秒写入一个字节(?)/字符(?(不确定是什么)

Filereader file = new Filereader("filename");
BufferedReader reader = new BufferedReader();
String line = "";

FileWriter fstream = new FileWriter("fName");
BufferedWriter fbw = new BufferedWriter(fstream);

while ((line = reader.readLine()) != null) {

    fbw.write(line + "\n");

}
你是说像这样的?从一个文件读取并写入另一个文件


你是说像这样的?从一个文件读取,然后写入另一个文件。

这基本上就是您想要的

try {
  FileOutputStream os = new FileOutputStream(FILENAME);
  while( true ){
     os.write(0);
     Thread.sleep(2000);  // wait 2 seconds before the next write
  }
}
catch( FileNotFoundException e ){
  System.err.println("watchdog error: " + e.getMessage())
  System.exit(1);
}
如果确实希望像C代码那样忽略所有“写入”错误,请将os.write更改为:

try {
  os.write(0);
}
catch( Exception we ){
  //ignoring write exception 
}

这基本上就是你想要的

try {
  FileOutputStream os = new FileOutputStream(FILENAME);
  while( true ){
     os.write(0);
     Thread.sleep(2000);  // wait 2 seconds before the next write
  }
}
catch( FileNotFoundException e ){
  System.err.println("watchdog error: " + e.getMessage())
  System.exit(1);
}
如果确实希望像C代码那样忽略所有“写入”错误,请将os.write更改为:

try {
  os.write(0);
}
catch( Exception we ){
  //ignoring write exception 
}

因此,
将“\0”
替换为整数
0
?您可以使用
\u0000
,因为空字符是字符设备。这会改变什么吗?实际上“\0”是字符串对象。它应该是“\0”(作为字符)或0(作为int)。注意,char被扩展为int,而不需要显式强制转换@贾维尔:这个设备是字符设备。所以
0
\0
是一样的?所以
“\0”
被替换为整数
0
?你可以使用
\u0000
,因为空字符是字符设备。这会改变什么吗?实际上“\0”是一个字符串对象。它应该是“\0”(作为字符)或0(作为int)。注意,char被扩展为int,而不需要显式强制转换@贾维尔:这个设备是字符设备。所以
0
\0
是一样的吗?它是字符设备。这会改变什么吗?不。这是一个冗余转换。它是一个字符设备。这会改变什么吗?不。它是一个冗余转换。