java如何从数据库读取数据并写入txt文件?

java如何从数据库读取数据并写入txt文件?,第1张

写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()  

        }  

    }  

}

1).按行读取TXT文件

package zc

import java.io.BufferedReader

import java.io.File

import java.io.FileNotFoundException

import java.io.FileReader

import java.io.IOException

public class readLine {

public static void main(String[] args) {

// TODO Auto-generated method stub

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

BufferedReader reader = null

String tempString = null

int line =1

try {

System.out.println("以行为单位读取文件内容,一次读一整行:")

reader = new BufferedReader(new FileReader(file))

while ((tempString = reader.readLine()) != null) {

System.out.println("Line"+ line + ":" +tempString)

line ++

}

reader.close()

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}finally{

if(reader != null){

try {

reader.close()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

}

}

}

2).按字节读取TXT文件

package zc

import java.io.File

import java.io.FileInputStream

import java.io.FileNotFoundException

import java.io.IOException

import java.io.InputStream

public class readerFileByChars {

public static void main(String[] args) {

// TODO Auto-generated method stub

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

InputStream in = null

byte[] tempByte = new byte[1024]

int byteread = 0

try {

System.out.println("以字节为单位读取文件内容,一次读多个字节:")

in = new FileInputStream(file)

while ((byteread = in.read(tempByte)) != -1 ) {

System.out.write(tempByte, 0, byteread)

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}finally{

if (in != null) {

try {

in.close()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

}

}

}

主要有用到java原生态的Io类,没有第三个包。直接上代码:

import java.io.*

public class write {

    public static void main(String[] args) {

        write("E://123.txt", "hello")

    }

    public static void write(String path, String content) {

        try {

            File f = new File(path)

            

            if (f.exists()) {

                System.out.println("文件存在")

            } else {

                System.out.println("文件不存在,正在创建...")

                if (f.createNewFile()) {

                    System.out.println("文件创建成功!")

                } else {

                    System.out.println("文件创建失败!")

                }

            }

            BufferedWriter output = new BufferedWriter(new FileWriter(f))

            output.write(content)

            output.close()

        } catch (Exception e) {

            e.printStackTrace()

        }

    }

}


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

原文地址:https://54852.com/tougao/11869495.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存