资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

JAVA往文件写入代码,java写入文本文件

跪求Java中写入文件和从文件中读取数据的最佳的代码!

import java.io.BufferedReader;

在三河等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、网站制作 网站设计制作按需求定制制作,公司网站建设,企业网站建设,品牌网站设计,营销型网站建设,成都外贸网站建设公司,三河网站建设费用合理。

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class IOTest {

public static void main(String[] args) {

String str = "123\r\n456";

writeFile(str);//写

String str1 = readFile();//读

System.out.println(str1);

}

/**

* 传递写的内容

* @param str

*/

static void writeFile(String str) {

try {

File file = new File("d:\\file.txt");

if(file.exists()){//存在

file.delete();//删除再建

file.createNewFile();

}else{

file.createNewFile();//不存在直接创建

}

FileWriter fw = new FileWriter(file);//文件写IO

fw.write(str);

fw.flush();

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 返回读取的内容

* @return

*/

static String readFile() {

String str = "", temp = null;

try {

File file = new File("d:\\file.txt");

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);//文件读IO

while((temp = br.readLine())!=null){//读到结束为止

str += (temp+"\n");

}

br.close();

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

return str;

}

}

刚写的,够朋友好好学习一下啦,呵呵

多多看API,多多练习

java如何对文件追加写入

java文件追加内容的三种方法:

方法一:

public static void writeToTxtByRandomAccessFile(File file, String str){

RandomAccessFile randomAccessFile = null;

try {

randomAccessFile = new RandomAccessFile(file,"rw");

long len = randomAccessFile.length();

randomAccessFile.seek(len);

randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n");

} catch (FileNotFoundException e) {

e.printStackTrace();

}catch (IOException e) {

e.printStackTrace();

}finally{

try {

randomAccessFile.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

方法二:

public static void writeToTxtByFileWriter(File file, String content){

BufferedWriter bw = null;

try {

FileWriter fw = new FileWriter(file, true);

bw = new BufferedWriter(fw);

bw.write(content);

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

方法三:

public static void writeToTxtByOutputStream(File file, String content){

BufferedOutputStream bufferedOutputStream = null;

try {

bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));

bufferedOutputStream.write(content.getBytes());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch(IOException e ){

e.printStackTrace();

}finally{

try {

bufferedOutputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

java如何追加写入txt文件

java追加写入txt文件代码及注释参考如下:

public void m() {

FileWriter ff= null;

try {

//查看C盘是否有a.txt文件来判定是否创建

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

ff = new FileWriter(f, true);//将字节写入文件末尾处,相当于追加信息。

} catch (IOException e) {

e.printStackTrace();

}

PrintWriter p = new PrintWriter(ff);

p.println("这里就可以写入要追加的内容了");//此处为追加内容

p.flush();

ff.try {

f.flush();

p.close();

ff.close();

} catch (IOException e) {

e.printStackTrace();

}

}

java代码 如何向TXT文件写入内容?

向txt文件写入内容基本思路就是获得一个file对象,新建一个txt文件,打开I/O操作流,使用写入方法进行读写内容,示例如下:

package common;

import java.io.*;

import java.util.ArrayList;

public class IOTest {

public static void main (String args[]) {

ReadDate();

WriteDate();

}

/**

* 读取数据

*/

public static void ReadDate() {

String url = “e:/2.txt”;

try {

FileReader read = new FileReader(new File(url));

StringBuffer sb = new StringBuffer();

char ch[] = new char[1024];

int d = read.read(ch);

while(d!=-1){

String str = new String(ch,0,d);

sb.append(str);

d = read.read(ch);

}

System.out.print(sb.toString());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 写入数据

*/

public static void WriteDate() {

try{

File file = new File(“D:/abc.txt”);

if (file.exists()) {

file.delete();

}

file.createNewFile();

BufferedWriter output = new BufferedWriter(new FileWriter(file));

ArrayList ResolveList = new ArrayList();

for (int i = 0; i  10; i++) {

ResolveList.add(Math.random()* 100);

}

for (int i=0 ;i 

output.write(String.valueOf(ResolveList.get(i)) + “\n”);

}

output.close();

} catch (Exception ex) {

System.out.println(ex);

}

}

}

原文出自【比特网】,转载请保留原文链接:

使用java如何直接往word文件中写入内容最好有详细的介绍和源代码

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PrintStream;

// 在当前路径(默认)创建3个非空.doc(当然也可以是.txt,.java…)文件

public class FileTest

{

//遇到异常抛给Java虚拟机处理

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

{

//i-1,恒为true,创建无限文件,就成病毒了

for (int i = 0; i 3; i++)

{

//指定要要输入内容的文件名name

String name = "a" + i + ".doc";

//定义一个节点输出流FilOutputStream

//通俗点:定义一个水管,水管通向name文件

FileOutputStream out = new FileOutputStream(name);

//使用PrintStream包装该节点流,使用PrintStream来输出字符串

//通俗点:给水管加个水龙头(PrintStream),这个水龙头具有放水功能(ps.print())

PrintStream ps = new PrintStream(out);

ps.print("我我我窝窝窝窝窝窝窝窝哦我");

ps.append("你");

}

}

}

一切尽在代码里!

JAVA中如何向创建一个文件并且向其中写入内容

如下代码,向D盘temp文件夹下的 test.txt 文件中写入 Hello World!

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

public class App {

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

File file = new File("d:\\temp\\test.txt");

try ( FileOutputStream outputStream = new FileOutputStream(file);

OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream) ) {

streamWriter.write("Hello World!");

streamWriter.flush();

}

}

}


网站名称:JAVA往文件写入代码,java写入文本文件
分享链接:http://cdkjz.cn/article/hdihhe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220