关于java写入txt的信息

博主:adminadmin 2022-11-22 13:02:11 63

今天给各位分享java写入txt的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java怎样把一字符串数组写入.txt文件中?

import java.io.File;\x0d\x0aimport java.io.OutputStream;\x0d\x0aimport java.io.FileOutputStream;\x0d\x0apublic class TestFile {\x0d\x0apublic static void main(String[] args) throws Exception{\x0d\x0a//在d盘上创建一个名为testfile的文本文件\x0d\x0aFile f = new File("D:"+File.separator+"testfile.txt");\x0d\x0a//用FileOutputSteam包装文件,并设置文件可追加\x0d\x0aOutputStream out = new FileOutputStream(f,true);\x0d\x0a//字符数组\x0d\x0aString[] str = {"shanghai","beijing","guangdong","xiamen"};\x0d\x0afor(int i =0; i

java 怎么将数据写入TXT文件

定义一个输出文件,然后输出就可以了,具体见下面的代码

 import java.io.*;

 public class StreamDemo

 {

  public static void main(String args[])

  {

   File f = new File("c:\\temp.txt") ;

   OutputStream out = null ;

   try 

   {

    out = new FileOutputStream(f) ;

   } 

   catch (FileNotFoundException e) 

   {

    e.printStackTrace();

   }

   // 将字符串转成字节数组

   byte b[] = "Hello World!!!".getBytes() ;

   try 

   {

    // 将byte数组写入到文件之中

    out.write(b) ;

   } 

   catch (IOException e1) 

   {

    e1.printStackTrace();

   }

   try 

   {

    out.close() ;

   } 

   catch (IOException e2) 

   {

    e2.printStackTrace();

   }

  

   // 以下为读文件操作

   InputStream in = null ;

   try 

   {

    in = new FileInputStream(f) ;

   } 

   catch (FileNotFoundException e3) 

   {

    e3.printStackTrace();

   }

   // 开辟一个空间用于接收文件读进来的数据

   byte b1[] = new byte[1024] ;

   int i = 0 ;

   try 

   {

   // 将b1的引用传递到read()方法之中,同时此方法返回读入数据的个数

    i = in.read(b1) ;

   } 

   catch (IOException e4) 

   {

    e4.printStackTrace();

   }

   try 

   {

    in.close() ;

   } 

   catch (IOException e5) 

   {

    e5.printStackTrace();

   }

   //将byte数组转换为字符串输出

   System.out.println(new String(b1,0,i)) ;

  }

 }

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

写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如何追加写入txt文件

 java中,对文件进行追加内容操作的三种方法!

import java.io.BufferedWriter;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.io.RandomAccessFile;

//如果文件存在,则追加内容;如果文件不存在,则创建文件,追加内容的三种方法

public class AppendContentToFile {

@SuppressWarnings("static-access")

public static void main(String[] args) {

AppendContentToFile a = new AppendContentToFile();

a.method1();

a.method2("E:\\dd.txt", "222222222222222");

a.method3("E:\\dd.txt", "33333333333");

}

方法1:

public void method1() {

FileWriter fw = null;

try {

//如果文件存在,则追加内容;如果文件不存在,则创建文件

File f=new File("E:\\dd.txt");

fw = new FileWriter(f, true);

} catch (IOException e) {

e.printStackTrace();

}

PrintWriter pw = new PrintWriter(fw);

pw.println("追加内容");

pw.flush();

try {

fw.flush();

pw.close();

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

方法2:

public static void method2(String file, String conent) {

BufferedWriter out = null;

try {

out = new BufferedWriter(new OutputStreamWriter(

new FileOutputStream(file, true)));

out.write(conent+"\r\n");

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

方法3:

public static void method3(String fileName, String content) {

try {

// 打开一个随机访问文件流,按读写方式

RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");

// 文件长度,字节数

long fileLength = randomFile.length();

// 将写文件指针移到文件尾。

randomFile.seek(fileLength);

randomFile.writeBytes(content+"\r\n");

randomFile.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

java如何写入txt文件

用另一个构造方法

FileWriter fileWriter=new FileWriter("c:\\Result.txt", true); // true代表追加

同理

BufferedWriter writer = new BufferedWriter(new FileWriter(new File("c:\\Result.txt"), true));

JAVA 写入TXT文件

在pw.write(s),后面加入pw.flush()即可。

在建立printWriter实例时(PrintWrite pw = new PrintWrite(fos,,true)),需要用boolean型指定,是不是自动刷新,如果没有指定自动刷新,则需要自己来flush.

呵,呵,要讲清楚啦。

楼主我只要50分额。

关于java写入txt和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。