java errorfile,Java中的File.createTempFile获取不兼容的类型错误

Till now my code works fine where I am creating file in temporary directory and processing it.

But now I am trying to provide specific directory where I actually want to create xml file. So in method createTmpXmlFile

private static Path createTmpXmlFile(final String prefix) {

try {

log.info("Creating temporary file {}{}", prefix, XML_SUFFIX);

return Files.createTempFile(Paths.get(gleifZipFile), prefix, XML_SUFFIX);

} catch (IOException e) {

throw new IllegalStateException("Could not create tmp file at " + prefix + XML_SUFFIX + ". ", e);

}

}

I changed from

return Files.createTempFile(prefix, XML_SUFFIX);

to

return File.createTempFile(prefix, XML_SUFFIX, "/tmp/in");

and I get following error:

java: incompatible types: java.lang.String cannot be converted to java.io.File.

If I change the logic here then its affecting other method that are calling createTmpXmlFile method.

I really don't understand how to resolve this issue. Below is my code:

@Slf4j

public class InputCS implements Runnable {

public static final String XML_SUFFIX = ".xml";

@Value("${gleifdataimporter.file.dir}")

private String gleifZipFile;

private void processleifZipFile() {

final AtomicBoolean isInsideLeiRecord = new AtomicBoolean();

isInsideLeiRecord.set(false);

final StringBuilder currentLeiRecordXml = new StringBuilder();

try (FileSystem zipFs = FileSystems.newFileSystem(jobRunner.getInputZipPath(), null)) {

Path tmpXMLPath = xmlFileFromLeiZipFile(zipFs);

try (Stream lines = Files.lines(tmpXMLPath)) {

AtomicInteger processedLinesCounter = new AtomicInteger();

AtomicInteger currentLineNumber = new AtomicInteger();

lines.sequential().forEach(handleLineAndIncrementLineNumber(isInsideLeiRecord, currentLeiRecordXml, processedLinesCounter, currentLineNumber));

log.info("{} lines of XML file inside LEIF input ZIP file {} processed.", processedLinesCounter.get(), jobRunner.getInputZipPath());

}catch (IOException e) {

throw new IllegalStateException("Problem reading input file at " + jobRunner.getInputZipPath() + ".", e);

} finally {

Files.delete(tmpXMLPath);

}

} catch (IOException e) {

throw new IllegalStateException("Problem reading input file at " + jobRunner.getInputZipPath() + ".", e);

}

}

private Path xmlFileFromLeiZipFile(FileSystem zipFs) { //extracts the xml file from zip file

log.info("Input file {} exists: {}", jobRunner.getInputZipPath(), Files.exists(jobRunner.getInputZipPath()));

Path tmpXmlPath = createTmpXmlFile("leif__" + System.currentTimeMillis());

for (Path rootDir : zipFs.getRootDirectories()) {

try (Stream files = treeAt(rootDir)) {

log.info("Trying to extract LEIF XML file from ZIP file into {}.", tmpXmlPath);

final Path xmlFileInsideZip = files

.filter(isNotADir())

.filter(Files::isRegularFile)

.findFirst()

.orElseThrow(() -> new IllegalStateException("No file found in LEI ZIP file."));

log.info("Path to LEIF XML file inside ZIP file: {}.", xmlFileInsideZip);

return copyReplacing(xmlFileInsideZip, tmpXmlPath);

}

}

throw new IllegalStateException("No file found in LEI ZIP file " + jobRunner.getInputZipPath() + ".");

}

private static Path createTmpXmlFile(final String prefix) {

try {

log.info("Creating temporary file {}{}", prefix, XML_SUFFIX);

return Files.createTempFile(Paths.get(gleifZipFile), prefix, XML_SUFFIX);

} catch (IOException e) {

throw new IllegalStateException("Could not create tmp file at " + prefix + XML_SUFFIX + ". ", e);

}

}

@NotNull

private static Path copyReplacing(Path from, Path to) {

requireNonNull(from, "Trying to copy from a path, which is null to path " + to + "."); //trying to copy file where no xml file exist in root directory

requireNonNull(to, "Trying to copy from path " + from + " to a path, which is null.");

try {

return Files.copy(from, to, REPLACE_EXISTING);

} catch (IOException e) {

throw new IllegalStateException("Cannot copy from " + from + " to " + to + ". ", e);

}

}

}

解决方案

As suggested by Slaw, use Files#createTempFile(Path,String,String,FileAttribute...) to specify the directory to create temp file.

Use Paths#get(String,String...) for java 7 or 8, or Path#of(String,String...) for java 11 or later to convert String to Path. Further reading: Paths.get vs Path.of

private static Path createTmpXmlFile(final String prefix) {

try {

// Java 11 or later

// return Files.createTempFile(Path.of("/tmp/in"), prefix, XML_SUFFIX);

// Java 8

return Files.createTempFile(Paths.get("/tmp/in"), prefix, XML_SUFFIX);

} catch (IOException e) {

throw new IllegalStateException("Could not create tmp file at " + prefix + XML_SUFFIX + ". ", e);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值