本文目錄一覽:
- 1、Java的文件生成然後寫入
- 2、Java讀寫文件的幾種方法
- 3、JAVA中如何將生成的數據寫入到文件中?
- 4、java代碼 如何向TXT文件寫入內容?
- 5、java如何寫入文件
- 6、java 怎麼將數據寫入TXT文件
Java的文件生成然後寫入
創建文件
new File(文件路徑).createNewFile();這是寫入文件
try{
FileOutputStream filewrite = new FileOutputStream(文件路徑); //這個要捕捉異常
filewrite.write(要寫入的數據byte[]);
filewrite.close();
}catch(IOException e);
Java讀寫文件的幾種方法
java讀取配置文件的幾種方法如下:
方式一:採用ServletContext讀取,讀取配置文件的realpath,然後通過文件流讀取出來。因為是用ServletContext讀取文件路徑,所以配置文件可以放入在web-info的classes目錄中,也可以在應用層級及web-info的目錄中。文件存放位置具體在eclipse工程中的表現是:可以放在src下面,也可放在web-info及webroot下面等。因為是讀取出路徑後,用文件流進行讀取的,所以可以讀取任意的配置文件包括xml和properties。缺點:不能在servlet外面應用讀取配置信息。
方式二:採用ResourceBundle類讀取配置信息,
優點是:可以以完全限定類名的方式載入資源後,直接的讀取出來,且可以在非Web應用中讀取資源文件。缺點:只能載入類classes下面的資源文件且只能讀取.properties文件。
JAVA中如何將生成的數據寫入到文件中?
package com.pig.database.file.txt;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
/**
* @author zhuruhong
*
* 若要變更這個產生的類別註解的範本,請移至
* 視窗 喜好設定 Java 程式碼產生 程式碼和註解
*/
public class WriteTxtFileByName {
private String filename = null;
public WriteTxtFileByName(String filename) {
this.filename = filename;
}
public void writeFileByName(String content) {
File docFile = new File(filename);
try {
docFile.createNewFile();
FileOutputStream txtfile = new FileOutputStream(docFile);
PrintStream p = new PrintStream(txtfile);
p.println(content);
txtfile.close();
p.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
WriteTxtFileByName wfbn = new WriteTxtFileByName(“title”);
wfbn.writeFileByName(“content”);
}
}
給你一個實例。
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如何寫入文件
package filewriter;
import java.io.FileWriter;
import java.io.IOException;
public class IOExceptionDemo {
private static final String LINE_SEPARATOR = System.getProperty(“line.separator”);
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter(“k:\\Demo.txt”, true);
fw.write(“hello” + LINE_SEPARATOR + “world!”);
} catch (Exception e) {
System.out.println(e.toString());
} finally {
if (fw != null)
try {
fw.close();
} catch (IOException e) {
throw new RuntimeException(“關閉失敗!”);
}
}
}
}
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)) ;
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/154877.html