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

前端 未结 9 1215
情歌与酒
情歌与酒 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: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 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 aLines, String aFileName) throws IOException {
            Path path = Paths.get(aFileName);
            Files.write(path, aLines, StandardCharsets.UTF_8);
        }
    }
    

    `

提交回复
热议问题