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()
}
}
向txt文件写入内容基本思路就是获得一个file对象,新建一个txt文件,打开I/O *** 作流,使用写入方法进行读写内容,示例如下:
package commonimport 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)
}
}
}
原文出自【比特网】,转载请保留原文链接:http://soft.chinabyte.com/database/303/12439303.shtml
import java.io.Fileimport java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.FileWriter
import java.io.IOException
import java.io.PrintStream
import java.io.PrintWriter
import java.io.RandomAccessFile
public class WriteStringToTxt {
public void WriteStringToFile(String filePath) {
try {
File file = new File(filePath)
PrintStream ps = new PrintStream(new FileOutputStream(file))
ps.println("http://www.jb51.net")// 往文件里写入字符串
ps.append("http://www.jb51.net")// 在已有的基础上添加字符串
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public void WriteStringToFile2(String filePath) {
try {
FileWriter fw = new FileWriter(filePath, true)
BufferedWriter bw = new BufferedWriter(fw)
bw.append("在已有的基础上添加字符串")
bw.write("abc\r\n ")// 往已有的文件上添加字符串
bw.write("def\r\n ")
bw.write("hijk ")
bw.close()
fw.close()
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public void WriteStringToFile3(String filePath) {
try {
PrintWriter pw = new PrintWriter(new FileWriter(filePath))
pw.println("abc ")
pw.println("def ")
pw.println("hef ")
pw.close()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public void WriteStringToFile4(String filePath) {
try {
RandomAccessFile rf = new RandomAccessFile(filePath, "rw")
rf.writeBytes("op\r\n")
rf.writeBytes("app\r\n")
rf.writeBytes("hijklllll")
rf.close()
} catch (IOException e) {
e.printStackTrace()
}
}
public void WriteStringToFile5(String filePath) {
try {
FileOutputStream fos = new FileOutputStream(filePath)
String s = "http://www.jb51.netl"
fos.write(s.getBytes())
fos.close()
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public static void main(String[] args) {
String filePath = "E:\\link.txt"
// new WriteStringToTxt().WriteStringToFile(filePath)
// new WriteStringToTxt().WriteStringToFile2(filePath)
// new WriteStringToTxt().WriteStringToFile3(filePath)
// new WriteStringToTxt().WriteStringToFile4(filePath)
new WriteStringToTxt().WriteStringToFile5(filePath)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)