tillnowmycodeworksfinewhereiamcreatingfileintemporarydirectoryandprocessingit。
butnowiamtryingtoprovidespecificdirectorywhereiactuallywanttocreatexmlfile.soinmethodcreatetmpxmlfile
私有状态参数创建器mpxmlfile (finalstringprefix ) {
try {
log.info (creatingtemporaryfile { } { },prefix,XML_SUFFIX );
return files.create tempfile (paths.get (gleifzipfile ),prefix,XML_SUFFIX );
}catch(ioexceptione ) {
thrownewillegalstateexception (' couldnotcreatetmpfileat ' prefix XML '.',e );
}
}
I changed from
return files.create tempfile (prefix,XML_SUFFIX;
到
return file.create tempfile (prefix,XML_SUFFIX,'/tmp/in ' );
and I get following error:
Java : incompatible types : Java.lang.stringcannotbeconvertedtojava.io.file。
ifichangethelogicherethenitsaffectingothermethodthatarecallingcreatetmpxmlfilemethod。
I really don ' tunderstandhowtoresolvethisissue.belowismycode :
@Slf4j
publicclassinputcsimplementsrunnable {
publicstaticfinalstringxml _ suffix='.XML ';
@value(($ ) gleifdataimporter.file.dir ) )
私有字符串格雷夫zipfile;
私有语音处理权限文件(
finalatomicbooleanisinsideleirecord=newatomicboolean (;
isinsideleirecord.set(false;
finalstringbuildercurrentleirecordxml=new stringbuilder (;
try (filesystemzipfs=file systems.new file system (job runner.getinputzippath (,null ) ) ) {
pathtmp XML path=xmlfilefromleizipfile (zipfs;
try (streamline s=files.lines (tmp XML path ) ) )
lines.sequential ().foreach (handlelineandincreandincrementlinenumber ) isinsideleirecordxml,currentLeiRecordXml,procesescor
log.info (linesofxmlfileinsideleifinputzipfile ) processed.)、processedLinesCounter.get )、jobrunner.getiitity
}catch(ioexceptione ) {
thrownewillegalstateexception (problemreadinginputfileat ' job runner.getinputzippath ),e );
} finally {
files.delete (tmp XML路径);
}
}catch(ioexceptione ) {
thrownewillegalstateexception (problemreadinginputfileat ' job runner.getinputzippath ),e );
}
}
privatepathxmlfilefromleizipfile (filesystemzipfs )//extractsthexmlfilefromzipfile
log.info(inputfile ) exists: )、jobRunner.getInputZipPath )、files.exists (job runner.getinputzippatath )、和
pathtmp XML path=createtmpxmlfile (' Leif _ _ ' system.current time millis );
path rootdir : zipfs.getrootdirectories () ) ) )。
try (流文件=treeat (rootdir ) ) }
log.info (tryingtoextractleifxmlfilefromzipfileinto )、tmpXmlPath );
finalpathxmlfileinsidezip=files
. filter(isnotadir ) )
. filter (files :3360 isregularfile )。
. findFirst () )
. orElseThrow ()-newillegalstateexception (' nofilefoundinleizipfile.' );
log.info (pathtoleifxmlFileInsideZipfile : { }.',xmlfileinsidezip );
返回复制(xmlfileinsidezip,tmp XML路径);
}
}
thrownewillegalstateexception (' nofilefoundinleizipfile ' job runner.getinputzippath ('.' ) );
}
私有状态参数创建器mpxmlfile (finalstringprefix ) {
try {
log.info (creatingtemporaryfile { } { },prefix,XML_SUFFIX );
return files.create tempfile (paths.get (gleifzipfile ),prefix,XML_SUFFIX );
}catch(ioexceptione ) {
thrownewillegalstateexception (' couldnotcreatetmpfileat ' prefix XML '.',e );
}
}
@NotNull
隐私保护(path from,Path to ) {
请求空值(from,' Trying to copy from a path,which is null to path ' to '.' ); //tryingtocopyfilewherenoxmlfileexistinrootdirectory
请求空值(to,' tryingtocopyfrompath ' from ' to apath,which is null.';
try {
return files.copy (从,到,REPLACE_EXISTING );
}catch(ioexceptione ) {
thrownewillegalstateexception (cannot copy from ' from ' to ' to '.',e );
}
}
}
As suggested by Slaw,usefiles#createtempfile(path,String,String,FileAttribute . ) tospecifythedirectorytocreatetetempppte
usepaths#get(String,String . ) for java 7 or 8,orpath#of ) String,string . (for Java 11 orlatertoconvertstringtopath . )
私有状态参数创建器mpxmlfile (finalstringprefix ) {
try {
//Java 11 or later
//return files.create tempfile (path.of )、prefix、XML_SUFFIX );
//Java 8
return files.create tempfile (paths.get ('/tmp/in ',prefix,XML_SUFFIX );
}catch(ioexceptione ) {
thrownewillegalstateexception (' couldnotcreatetmpfileat ' prefix XML '.',e );
}
}