JAVA中如何将生成的数据写入到文件中?

JAVA中如何将生成的数据写入到文件中?,第1张

packagecom.pig.database.file.txt

importjava.io.File

importjava.io.FileOutputStream

importjava.io.IOException

importjava.io.PrintStream

/**

*@authorzhuruhong

*

*若要变更这个产生的类别注解的范本,请移至

*视窗>喜好设定>Java>程式码产生>程式码和注解

*/

publicclassWriteTxtFileByName{

privateStringfilename=null

publicWriteTxtFileByName(Stringfilename){

this.filename=filename

}

publicvoidwriteFileByName(Stringcontent){

FiledocFile=newFile(filename)

try{

docFile.createNewFile()

FileOutputStreamtxtfile=newFileOutputStream(docFile)

PrintStreamp=newPrintStream(txtfile)

p.println(content)

txtfile.close()

p.close()

}catch(IOExceptione){

e.printStackTrace()

}

}

publicstaticvoidmain(String[]args){

WriteTxtFileByNamewfbn=newWriteTxtFileByName("title")

wfbn.writeFileByName("content")

}

}

给你一个实例。

写Java程序时经常碰到要读如txt或写入txt文件的情况,但是由于要定义好多变量,经常记不住,每次都要查,特此整理一下,简单易用,方便好懂!

[java] view plain copy

package edu.thu.keyword.test  

  

import java.io.File  

import java.io.InputStreamReader  

import java.io.BufferedReader  

import java.io.BufferedWriter  

import java.io.FileInputStream  

import java.io.FileWriter  

  

public class cin_txt {  

    static void main(String args[]) {  

        try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw  

  

            /* 读入TXT文件 */  

            String pathname = "D:\\twitter\\13_9_6\\dataset\\en\\input.txt" // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径  

            File filename = new File(pathname) // 要读取以上路径的input。txt文件  

            InputStreamReader reader = new InputStreamReader(  

                    new FileInputStream(filename)) // 建立一个输入流对象reader  

            BufferedReader br = new BufferedReader(reader) // 建立一个对象,它把文件内容转成计算机能读懂的语言  

            String line = ""  

            line = br.readLine()  

            while (line != null) {  

                line = br.readLine() // 一次读入一行数据  

            }  

  

            /* 写入Txt文件 */  

            File writename = new File(".\\result\\en\\output.txt") // 相对路径,如果没有则要建立一个新的output。txt文件  

            writename.createNewFile() // 创建新文件  

            BufferedWriter out = new BufferedWriter(new FileWriter(writename))  

            out.write("我会写入文件啦\r\n") // \r\n即为换行  

            out.flush() // 把缓存区内容压入文件  

            out.close() // 最后记得关闭文件  

  

        } catch (Exception e) {  

            e.printStackTrace()  

        }  

    }  

}

可以使用java中的FileWriter类向文件中写入数据。很简单。代码例子如下:

import java.io.FileWriter  

import java.io.IOException  

  

public class Filewriter {  

  

    private static final String LINE_SEPARATOR = System.getProperty("line.separator")  

  

    /** 

     *  

     * @param args 

     * @throws IOException  

     */  

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

        /** 

         * 创建一个可以往文件中写入字符数据的字符流输出流对象 

         * 创建时必须明确文件的目的地 

         * 如果文件不存在,这回自动创建。如果文件存在,则会覆盖。 

         * 当路径错误时会抛异常 

         *  

         * 当在创建时加入true参数,回实现对文件的续写。 

         */  

        FileWriter fw = new FileWriter("C:\\demo1.txt",false)  

        /** 

         * 调用该对象的write方法,向文件写入字符。 

         *  

         * 其实写入到了临时存储缓冲区中 

         */  

        fw.write("hello \r\nworld!")//windows中的换行为\r\n    unix下为\r。   

        fw.write("hahaha")  

        /** 

         * 进行刷新,将字符写到目的地中。 

         */  

//      fw.flush()  

        /** 

         * 关闭流,关闭资源。在关闭前会调用flush方法 刷新缓冲区。关闭后在写的话,会抛IOException 

         */  

        fw.close()  

          

  

    }  

  

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/8053305.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存