What is the simplest way to write a text file in Java?

前端 未结 9 1216
情歌与酒
情歌与酒 2021-01-01 09:55

I am wondering what is the easiest (and simplest) way to write a text file in Java. Please be simple, because I am a beginner :D

I searched the web and found this co

相关标签:
9条回答
  • 2021-01-01 10:23

    With Java 7 and up, a one liner using Files:

    String text = "Text to save to file";
    Files.write(Paths.get("./fileName.txt"), text.getBytes());
    
    0 讨论(0)
  • 2021-01-01 10:23

    Your code is the simplest. But, i always try to optimize the code further. Here is a sample.

    try (BufferedWriter bw = new BufferedWriter(new FileWriter(new File("./output/output.txt")))) {
        bw.write("Hello, This is a test message");
        bw.close();
        }catch (FileNotFoundException ex) {
        System.out.println(ex.toString());
        }
    
    0 讨论(0)
  • 2021-01-01 10:25

    Appending the file FileWriter(String fileName, boolean append)

    try {   // this is for monitoring runtime Exception within the block 
    
            String content = "This is the content to write into file"; // content to write into the file
    
            File file = new  File("C:/Users/Geroge/SkyDrive/Documents/inputFile.txt"); // here file not created here
    
            // if file doesnt exists, then create it
            if (!file.exists()) {   // checks whether the file is Exist or not
                file.createNewFile();   // here if file not exist new file created 
            }
    
            FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); // creating fileWriter object with the file
            BufferedWriter bw = new BufferedWriter(fw); // creating bufferWriter which is used to write the content into the file
            bw.write(content); // write method is used to write the given content into the file
            bw.close(); // Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect. 
    
            System.out.println("Done");
    
        } catch (IOException e) { // if any exception occurs it will catch
            e.printStackTrace();
        }
    
    0 讨论(0)
  • 2021-01-01 10:26
    File file = new File("path/file.name");
    IOUtils.write("content", new FileOutputStream(file));
    

    IOUtils also can be used to write/read files easily with java 8.

    0 讨论(0)
  • 2021-01-01 10:29

    You can use FileUtils from Apache Commons:

    import org.apache.commons.io.FileUtils;
    
    final File file = new File("test.txt");
    FileUtils.writeStringToFile(file, "your content", StandardCharsets.UTF_8);
    
    0 讨论(0)
  • 2021-01-01 10:31

    You could do this by using JAVA 7 new File API.

    code sample: `

    public class FileWriter7 {
        public static void main(String[] args) throws IOException {
            List<String> lines = Arrays.asList(new String[] { "This is the content to write into file" });
            String filepath = "C:/Users/Geroge/SkyDrive/Documents/inputFile.txt";
            writeSmallTextFile(lines, filepath);
        }
    
        private static void writeSmallTextFile(List<String> aLines, String aFileName) throws IOException {
            Path path = Paths.get(aFileName);
            Files.write(path, aLines, StandardCharsets.UTF_8);
        }
    }
    

    `

    0 讨论(0)
提交回复
热议问题