java 创建新文件_如何创建新文件?(How to create a new file?)

如何创建新文件?(How to create a new file?)

问题描述 (Problem Description)

如何创建新文件?

解决方案 (Solution)

此示例演示了使用File类的File()构造函数和file.createNewFile()方法创建新文件的方法。import java.io.File;

import java.io.IOException;

public class Main {

public static void main(String[] args) {

try {

File file = new File("C:/myfile.txt");

if(file.createNewFile())System.out.println("Success!");

else System.out.println ("Error, file already exists.");

}

catch(IOException ioe) {

ioe.printStackTrace();

}

}

}

结果 (Result)

上面的代码示例将产生以下结果(如果“myfile.txt之前不存在)Success!

以下是文件创建的另一个示例示例import java.io.IOException;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.nio.charset.StandardCharsets;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.nio.file.StandardOpenOption;

import java.util.Arrays;

import java.util.List;

public class JavaApplication1 {

public static void main(String[] args) throws IOException {

createFileUsingFileClass();

createFileUsingFileOutputStreamClass();

createFileIn_NIO();

// TODO code application logic here

}

private static void createFileUsingFileClass() throws IOException {

File file = new File("c://testFile1.txt");

//Create the file

if (file.createNewFile()) {

System.out.println("File is created!");

} else {

System.out.println("File already exists.");

}

//Write Content

FileWriter writer = new FileWriter(file);

writer.write("Test data");

writer.close();

}

private static void createFileUsingFileOutputStreamClass() throws IOException {

String data = "Test data";

FileOutputStream out = new FileOutputStream("c://testFile2.txt");

out.write(data.getBytes());

out.close();

}

private static void createFileIn_NIO() throws IOException {

String data = "Test data";

Files.write(Paths.get("c://testFile3.txt"), data.getBytes());

List lines = Arrays.asList("1st line", "2nd line");

Files.write(Paths.get(

"file6.txt"), lines, StandardCharsets.UTF_8,

StandardOpenOption.CREATE, StandardOpenOption.APPEND);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To split a DOCX file into multiple DOCX files by title using docx4j, you can follow these steps: 1. Load the original DOCX file using docx4j. 2. Iterate through the document's paragraphs and identify the paragraphs that represent titles/headings. You can use the paragraph's style or any other identifying feature to determine the titles. 3. For each title paragraph, create a new DOCX file. 4. Copy all the paragraphs from the original document until you encounter the next title paragraph. Add these paragraphs to the newly created DOCX file. 5. Save the newly created DOCX file. Here's a sample code snippet to demonstrate this process: ```java import org.docx4j.Docx4J; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.wml.Body; import org.docx4j.wml.Document; import org.docx4j.wml.P; import java.io.File; public class DocxSplitter { public static void main(String[] args) { try { // Load the original DOCX file WordprocessingMLPackage wordMLPackage = Docx4J.load(new File("input.docx")); Document document = wordMLPackage.getMainDocumentPart().getJaxbElement(); Body body = document.getBody(); // Iterate through paragraphs to split by title String currentTitle = null; WordprocessingMLPackage currentPackage = null; for (Object obj : body.getContent()) { if (obj instanceof P) { P paragraph = (P) obj; String style = paragraph.getPPr().getPStyle().getVal(); if (style.equals("Title")) { // Start a new DOCX file for the title if (currentPackage != null) { String fileName = currentTitle +

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值