Ⅰ java 中簡述使用流進行讀寫文本文件的步驟
InputStream
三個基本的讀方法
abstract int read() : 讀取一個位元組數據,並返回讀到的數據,如果返回-1,表示讀到了輸入流的末尾。
int read(byte[] b) : 將數據讀入一個位元組數組,同時返回實際讀取的位元組數。如果返回-1,表示讀到了輸入流的末尾。
int read(byte[] b, int off, int len) :將數據讀入一個位元組數組,同時返回實際讀取的位元組數。如果返回-1,表示讀到了輸入流的末尾。off指定在數組b中存放數據的起始偏移位置;len指定讀取的最大位元組數。
OutputStream
三個基本的寫方法
abstract void write(int b) :往輸出流中寫入一個位元組。
void write(byte[] b) :往輸出流中寫入數組b中的所有位元組。
void write(byte[] b, int off, int len) :往輸出流中寫入數組b中從偏移量off開始的len個位元組的數據。
其它方法
void flush() :刷新輸出流,強制緩沖區中的輸出位元組被寫出。
void close() :關閉輸出流,釋放和這個流相關的系統資源。
Ⅱ mac系統,java編程中文件流的路徑是如何寫的
看看這個,我昨天剛寫的: import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;public class AddList {
private String filePath = "";
private String bakPath = "";
private String content = "";
Scanner sc = new Scanner(System.in);
public String readFile(){
content = "";
if (isNull(filePath)) {
System.out.println("文件存儲路徑:");
filePath = sc.nextLine();
}
File file = new File(filePath);
FileReader fr = null;
try {
if (file.exists()) {
fr = new FileReader(file);
char[] chars = new char[1024];
int n = 0;
while((n = fr.read(chars)) != -1){
String string = new String(chars, 0, n);
content = content + string;
}
} else {
System.out.println("文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content;
}
public void writeFile(String path){
File file = new File(path);
FileOutputStream fos = null;
mkDirs(path);
try {
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintWriter pw = new PrintWriter(bos, true);
pw.print(content);
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void writeFile(){
if (isNull(filePath)) {
System.out.println("文件存儲路徑:");
filePath = sc.nextLine();
}
File file = new File(filePath);
FileOutputStream fos = null;
mkDirs(filePath);
try {
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintWriter pw = new PrintWriter(bos, true);
pw.print(content);
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void mkDirs(String filepath){
if (filepath.indexOf("\\") != -1) {
filepath = filepath.replaceAll("\\", "/");
}
int n = filepath.indexOf("//");
String path = filepath.substring(0, n) + "//";
filepath = filepath.substring(filepath.indexOf("//") + 1, filepath.length());
String[] files = filepath.split("/");
for (int i = 0; i < files.length - 1; i++) {
path = path + files[i];
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
}
}
public void addImfor(){
System.out.println("--------增加記錄---------");
String name = "";
String tel = "";
String email = "";
content = readFile();
while(true){
System.out.println("姓名:");
name = sc.next();
System.out.println("電話:");
tel = sc.next();
System.out.println("Email:");
email = sc.next();
content = content + name + "<>" + tel + "<>" + email +"<==>";
System.out.println("0、Exit 1、繼續");
int i = sc.nextInt();
if (i == 0) {
break;
}
}
writeFile();
}
public void deleteImfor(){
System.out.println("---------刪除記錄---------");
String name = "";
String[] imfors = null;
content = readFile();
while(true){
System.out.println("你要刪除的姓名是:");
name = sc.next();
if (content.indexOf(name) != -1) {
imfors = content.split("<==>");
for (int i = 0; i < imfors.length; i++) {
if (imfors[i].indexOf(name) != -1) {
imfors[i] = "";
}
}
content = "";
for (int i = 0; i < imfors.length; i++) {
if (!isNull(imfors[i])) {
content = content + imfors[i] + "<==>";
}
}
writeFile();
System.out.println("刪除成功");
} else {
System.out.println("此人不存在");
}
System.out.println("0、Exit 1、繼續");
int i = sc.nextInt();
if (i == 0) {
break;
}
}
}
public void viewAll(){
System.out.println("----------顯示所有------------");
content = readFile();
if (!isNull(content)) {
String[] imfors = content.split("<==>");
System.out.println("姓名\t電話\tEmail");
for (int i = 0; i < imfors.length; i++) {
String[] imfor = imfors[i].split("<>");
for (int j = 0; j < imfor.length; j++) {
System.out.print(imfor[j] + "\t");
}
System.out.println();
}
} else {
System.out.println("暫時還沒有記錄");
}
}
public void queryImfor(){
System.out.println("----------查找記錄-----------");
content = readFile();
if (!isNull(content)) {
String result = "";
String[] imfors = null;
String[] imfor = null;
String name = "";
boolean bool = false;
while(true){
result = "";
System.out.println("請輸入關鍵字(按姓名查找):");
name = sc.next();
bool = false;
if (content.indexOf(name) != -1) {
imfors = content.split("<==>");
for (int i = 0; i < imfors.length; i++) {
if (imfors[i].indexOf(name) != -1) {
imfor = imfors[i].split("<>");
if (imfor[0].equals(name)) {
bool = true;
result = result + imfors[i] + "<==>";
}
}
}
if (bool) {
imfors = result.split("<==>");
System.out.println("姓名\t電話\tEmail");
for (int i = 0; i < imfors.length; i++) {
imfor = imfors[i].split("<>");
for (int j = 0; j < imfor.length; j++) {
System.out.print(imfor[j] + "\t");
}
System.out.println();
}
} else {
System.out.println("無此人信息");
}
} else {
System.out.println("無此人信息");
}
System.out.println("0、Exit 1、繼續");
int i = sc.nextInt();
if (i == 0) {
break;
}
}
} else {
System.out.println("文件還沒有記錄");
}
}
public void (){
System.out.println("----------備份-----------");
content = readFile();
if (isNull(bakPath)) {
System.out.println("備份全路徑:");
bakPath = sc.next();
}
writeFile(bakPath);
System.out.println("備份成功");
}
public boolean isNull(String string){
if (null == string || "" == string || 0 == string.length()) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
AddList add = new AddList();
Scanner sc = new Scanner(System.in);
int operater = 0;
while(true){
System.out.println("選擇功能:\n1、增加記錄 2、刪除記錄 3、顯示所有 4、查詢記錄 5、備份 6、退出");
operater = sc.nextInt();
if (1 == operater) {
add.addImfor();
} else if (2 == operater) {
add.deleteImfor();
} else if (3 == operater) {
add.viewAll();
} else if (4 == operater) {
add.queryImfor();
} else if (5 == operater) {
add.();
} else if (6 == operater) {
System.out.println("謝謝使用");
break;
}
}
}
}
Ⅲ java文件輸出流,寫到.txt文件,如何實現換行
1 實現的方式總共有3中, 1)原始的 \r\n 方式
2)BufferedWriter的newline()方法
3) System.getProperty()方法
下面使用代碼逐一展示 , 寫到.txt文件實現換行
2 使用java中的轉義符"\r\n":
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
String str="aaa";
str+="\r\n";
fos.write(str);
3 BufferedWriter的newline()方法:
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
BufferedWriter bw=new BufferedWriter(fos);
bw.write("你好");
bw.newline(); //實現換行
4 System.getProperty()方法:
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
String str = "aaa"+System.getProperty("line.separator");
fos.write(str);
Ⅳ java中怎麼用io流讀寫文件
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
publicclassTest14{
publicstaticvoidmain(String[]args)throwsIOException{
StringfPath="C:/test.txt";
//讀
BufferedReaderbr=newBufferedReader(newFileReader(fPath));
System.out.println(br.readLine());
br.close();////使用後記得關閉
//寫
BufferedWriterbw=newBufferedWriter(newFileWriter(fPath));
bw.write("寫一段話到文件里");
bw.flush();
bw.close();//使用後記得關閉
}
}
Ⅳ java 中簡述使用流進行讀寫文本文件的步驟
一、Java IO學習基礎之讀寫文本文件
Java的IO操作都是基於流進行操作的,為了提高讀寫效率一般需要進行緩沖。
簡單的示常式序如下:
/**
* 讀出1.txt中的內容,寫入2.txt中
*
*/
import java.io.*;
public class ReadWriteFile{
public static void main(String[] args){
try{
File read = new File("c:\\1.txt");
File write = new File("c:\\2.txt");
BufferedReader br = new BufferedReader(
new FileReader(read));
BufferedWriter bw = new BufferedWriter(
new FileWriter(write));
String temp = null;
temp = br.readLine();
while(temp != null){
//寫文件
bw.write(temp + "\r\n"); //只適用Windows系統
//繼續讀文件
temp = br.readLine();
}
bw.close();
br.close();
}catch(FileNotFoundException e){ //文件未找到
System.out.println (e);
}catch(IOException e){
System.out.println (e);
}
}
}
以上是一個比較簡單的基礎示例。本文上下兩部分都是從網上摘抄,合並在一起,方便下自己以後查找。
二、Java IO學習筆記+代碼
文件對象的生成和文件的創建
/*
* ProcessFileName.java
*
* Created on 2006年8月22日, 下午3:10
*
* 文件對象的生成和文件的創建
*/
package study.iostudy;
import java.io.*;
public class GenerateFile
{
public static void main(String[] args)
{
File dirObject = new File("d:\\mydir");
File fileObject1 = new File("oneFirst.txt");
File fileObject2 = new File("d:\\mydir", "firstFile.txt");
System.out.println(fileObject2);
try
{
dirObject.mkdir();
}catch(SecurityException e)
{
e.printStackTrace();
}
try
{
fileObject2.createNewFile();
fileObject1.createNewFile();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
文件名的處理
/*
* ProcessFileName.java
*
* Created on 2006年8月22日, 下午3:29
*
* 文件名的處理
*/
package study.iostudy;
import java.io.*;
/*
* 文件名的處理
* String getName(); 獲得文件的名稱,不包含文件所在的路徑。
* String getPath(); 獲得文件的路徑。
* String getAbsolutePath(); 獲得文件的絕對路徑。
* String getParent(); 獲得文件的上一級目錄的名稱。
* String renameTo(File newName); 按參數中給定的完整路徑更改當前的文件名。
* int compareTo(File pathName); 按照字典順序比較兩個文件對象的路徑。
* boolean isAbsolute(); 測試文件對象的路徑是不是絕對路徑。
*/
public class ProcesserFileName
{
public static void main(String[] args)
{
File fileObject1 = new File("d:\\mydir\\firstFile.txt");
File fileObject2 = new File("d:\\firstFile.txt");
boolean pathAbsolute = fileObject1.isAbsolute();
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
System.out.println("There are some information of fileObject1's file name:");
System.out.println("fileObject1: " + fileObject1);
System.out.println("fileObject2: " + fileObject2);
System.out.println("file name: " + fileObject1.getName());
System.out.println("file path: " + fileObject1.getPath());
System.out.println("file absolute path: " + fileObject1.getAbsolutePath());
System.out.println("file's parent directory: " + fileObject1.getParent());
System.out.println("file's absoulte path: " + pathAbsolute);
int sameName = fileObject1.compareTo(fileObject2);
System.out.println("fileObject1 compare to fileObject2: " + sameName);
fileObject1.renameTo(fileObject2);
System.out.println("file's new name: " + fileObject1.getName());
}
}
測試和設置文件屬性
/*
* SetterFileAttribute.java
*
* Created on 2006年8月22日, 下午3:51
*
* 測試和設置文件屬性
*/
package study.iostudy;
import java.io.*;
public class SetterFileAttribute
{
/*
* File類中提供的有關文件屬性測試方面的方法有以下幾種:
* boolean exists(); 測試當前文件對象指示的文件是否存在。
* boolean isFile(); 測試當前文件對象是不是文件。
* boolean isDirectory(); 測試當前文件對象是不是目錄。
* boolean canRead(); 測試當前文件對象是否可讀。
* boolean canWrite(); 測試當前文件對象是否可寫。
* boolean setReadOnly(); 將當前文件對象設置為只讀。
* long length(); 獲得當前文件對象的長度。
*/
public static void main(String[] args)
{
File dirObject = new File("d:\\mydir");
File fileObject = new File("d:\\mydir\\firstFile.txt");
try
{
dirObject.mkdir();
fileObject.createNewFile();
}catch(IOException e)
{
e.printStackTrace();
}
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
System.out.println("there are some information about property of file object:");
System.out.println("file object : " + fileObject);
System.out.println("file exist? " + fileObject.exists());
System.out.println("Is a file? " + fileObject.isFile());
System.out.println("Is a directory?" + fileObject.isDirectory());
System.out.println("Can read this file? " + fileObject.canRead());
System.out.println("Can write this fie? " + fileObject.canWrite());
long fileLen = fileObject.length();
System.out.println("file length: " +fileLen);
boolean fileRead = fileObject.setReadOnly();
System.out.println(fileRead);
System.out.println("Can read this file? " + fileObject.canRead());
System.out.println("Can write this fie? " + fileObject.canWrite());
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
}
}
文件操作方法
/*
* FileOperation.java
*
* Created on 2006年8月22日, 下午4:25
*
* 文件操作方法
*/
package study.iostudy;
import java.io.*;
/*
* 有關文件操作方面的方法有如下幾種:
* boolean createNewFile(); 根據當前的文件對象創建一個新的文件。
* boolean mkdir(); 根據當前的文件對象生成一目錄,也就是指定路徑下的文件夾。
* boolean mkdirs(); 也是根據當前的文件對象生成一個目錄,
* 不同的地方在於該方法即使創建目錄失敗,
* 也會成功參數中指定的所有父目錄。
* boolean delete(); 刪除當前的文件。
* void deleteOnExit(); 當前Java虛擬機終止時刪除當前的文件。
* String list(); 列出當前目錄下的文件。
*/
public class FileOperation
* 找出一個目錄下所有的文件
package study.iostudy;
import java.io.*;
public class SearchFile
{
public static void main(String[] args)
{
File dirObject = new File("D:\\aa");
Filter1 filterObj1 = new Filter1("HTML");
Filter2 filterObj2 = new Filter2("Applet");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("list HTML files in directory: " + dirObject);
String[] filesObj1 = dirObject.list(filterObj1);
for (int i = 0; i < filesObj1.length; i++)
{
File fileObject = new File(dirObject, filesObj1[i]);
System.out.println(((fileObject.isFile())
? "HTML file: " : "sub directory: ") + fileObject);
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
String[] filesObj2 = dirObject.list(filterObj2);
for (int i = 0; i < filesObj2.length; i++)
{
File fileObject = new File(dirObject, filesObj2[i]);
System.out.println(((fileObject.isFile())
? "htm file: " : "sub directory: ") + fileObject);
}
}
}
class Filter1 implements FilenameFilter
{
String fileExtent;
Filter1(String extentObj)
{
fileExtent = extentObj;
}
public boolean accept(File dir, String name)
{
return name.endsWith("." + fileExtent);
}
}
class Filter2 implements FilenameFilter
{
String fileName;
Filter2(String fileName)
{
this.fileName = fileName;
字元流處理
* ProcesserCharacterStream.java
* 字元流處理
*
* java.io包中加入了專門用於字元流處理的類,這些類都是Reader和Writer類的子類,
* Reader和Writer是兩個抽象類,只提供了一系列用於字元流處理的介面,不能生成這
* 兩個類的實例。
* java.io包中用於字元流處理的最基本的類是InputStreamReader和OutputStreamWriter,
* 用來在位元組流和字元流之間作為中介。
*
* 下面是InputStreamReader類和OutputStreamWriter類的常用方法:
*
* public InputStreamReader(InputStream in)
* 根據當前平台預設的編碼規范,基於位元組流in生成一個輸入字元流。
* public InputStreamReader(InputStream in, String sysCode)throws UnSupportedEncodingException
* 按照參數sysCode指定的編碼規范,基於位元組流in構造輸入字元流,如果不支持參數sysCode中指定的編碼規范,就會產生異常。
* public OutputStreamWriter(OutputStream out)
* 根據當前平台預設的編碼規范,基於位元組流out生成一個輸入字元流。
* public OutputStreamWriter(OutputStream out, String sysCode) throws UnsupportedEncodingException
* 按照參數sysCode指定的編碼規范,基於位元組流out構造輸入字元流,如果不支持參數sysCode中指定的編碼規范,就會產生異常。
* public String getEncoding()
* 獲得當前字元流使用的編碼方式。
* public void close() throws IOException
* 用於關閉流。
* public int read() throws IOException
* 用於讀取一個字元。
* public int read(char[] cbuf, int off, int len)
* 用於讀取len個字元到數組cbuf的索引off處。
* public void write(char[] cbuf, int off, int len) throws IOException
* 將字元數組cbuf中從索引off處開始的len個字元寫入輸出流。
* public void write(int c) throws IOException
* 將單個字元寫入輸入流。
* public void write(String str, int off, int len) throws IOException
* 將字元串str中從索引off位置開始的ltn個字元寫入輸出流。
*
* 此外,為了提高字元流處理的效率,在Java語言中,引入了BufferedReader和BufferWriter類,這兩個類對字元流進行塊處理。
* 兩個類的常用方法如下:
* public BufferedReader(Reader in)
* 用於基於普通字元輸入流in生成相應的緩沖流。
* public BufferedReader(Reader in, int bufSize)
* 用於基於普通字元輸入流in生成相應的緩沖流,緩沖區大小為參數bufSize指定。
* public BufferedWriter(Writer out)
* 用於基於普通字元輸入流out生成相應的緩沖流。
* public BufferedWriter(Writer out, int bufSize)
* 用於基於普通字元輸入流out生在相應緩沖流,緩沖流大小為參數bufSize指定。
* public String readLine() throws IOException
* 用於從輸入流中讀取一行字元。
* public void newLine() throws IOException
* 用於向字元輸入流中寫入一行結束標記,值得注意的是,該標記不是簡單的換行符"\n",而是系統定義的屬性line.separator。
在上面的程序中,我們首先聲明了FileInputStream類對象inStream和
* FileOutputStream類的對象outStream,接著聲明了BufferInputStream
* 類對象bufObj、BufferedOutputStream類對象bufOutObj、
* DataInputStream類對象dataInObj以及PushbackInputStream類對象pushObj,
* 在try代碼塊中對上面這些對象進行初始化,程序的目的是通過BufferedInputStream
* 類對象bufInObj和BufferedOutputStream類對象bufOutObj將secondFile.txt
* 文件中內容輸出到屏幕,並將該文件的內容寫入thirdFile.txt文件中,值得注意的是,
* 將secondFile.txt文件中的內容輸出之前,程序中使用
* "System.out.println(dataInObj.readBoolean());" 語句根據readBoolean()結果
* 輸出了true,而secondFile.txt文件開始內容為「Modify」,和一個字元為M,
* 因此輸出的文件內容沒有「M」字元,thirdFile.txt文件中也比secondFile.txt
* 文件少第一個字元「M」。隨後,通過PushbackInputStream類對象pushObj讀取
* thirdFile.txt文件中的內容,輸出讀到的字元,當讀到的不是字元,輸出回車,將字元
* 數組pushByte寫回到thirdFile.txt文件中,也就是「ok」寫迴文件中。
* 對象串列化
* 對象通過寫出描述自己狀態的數值來記錄自己,這個過程叫做對象串列化。對象的壽命通
* 常是隨著生成該對象的程序的終止而終止,在有些情況下,需要將對象的狀態保存下來,然後
* 在必要的時候將對象恢復,值得注意的是,如果變數是另一個對象的引用,則引用的對象也要
* 串列化,串列化是一個遞歸的過程,可能會涉及到一個復雜樹結構的串列化,比如包括原有對
* 象,對象的對象等。
* 在java.io包中,介面Serializable是實現對象串列化的工具,只有實現了Serializable
* 的對象才可以被串列化。Serializable介面中沒有任何的方法,當一個類聲明實現Seriali-
* zable介面時,只是表明該類遵循串列化協議,而不需要實現任何特殊的方法。
* 在進行對象串列化時,需要注意將串列化的對象和輸入、輸出流聯系起來,首先通過對
* 象輸出流將對象狀態保存下來,然後通過對象輸入流將對象狀態恢復。
Ⅵ Java流的文件寫入問題!
使用 java.util.Sanner sc=new java.util.Sanner(System.in);
sc.nextLine();//輸入一行字元串,Enter結束
//該方法僅能讀取UTF格式的字元,而鍵盤的不能輸入UTF-8格式字元,就是你按Enter鍵時,按照UTF-8的格式,它並不是Enter
UTF-8 修改版
DataInput 和 DataOutput 介面的實現表示稍作改版的 UTF-8 格式的 Unicode 字元串。(關於標准 UTF-8 格式的信息,請參閱 The Unicode Standard, Version 4.0 的 3.9 Unicode Encoding Forms 節)。注意,在下表中,最高有效位顯示在最左邊的列中。
這種格式與標准 UTF-8 格式之間的不同如下:
* null 位元組 '\u0000' 是用 2-byte 格式而不是 1-byte 格式編碼的,因此已編碼的字元串中決不會有嵌入的 null。
* 僅使用 1-byte、2-byte 和 3-byte 格式。
* 增補字元是以代理項對的形式表示的。
Ⅶ java中關於文件流的讀寫(Writer Reader)
你好 我剛剛做了個例子 方便你看
class_writer class 這個例子是從 一個文件中讀取數據然後插入了資料庫中 你可以只看讀取與插入的過程 希望能幫到你.
public class writer {
public boolean writ(String str) {
boolean success=false;
str = str.replaceAll(" ", "").replaceAll("\"", "");
String[] strs = str.split(",");
BaseJDBC base = new BaseJDBC();
String ML = "";
Statement stmt;
Connection conn;
ML = "'"+strs[0].replace(" ", "").trim() + "','"
+ strs[1].replace(" ", "").trim() + "','"
+ strs[2].replace(" ", "").trim() + "','"
+ strs[3].replace(" ", "").trim() + "','"
+ strs[4].replace(" ", "").trim()+ "','"
+ strs[5].replace(" ", "").trim()+"','"
+ strs[6].replace(" ", "").trim()+"'";
String query = "INSERT INTO BANK_INFO VALUES(" + ML + ")";
if (!strs[0].equals("參與者行號")) {
try {
conn=base.genConn();
stmt = conn.createStatement();
int num=stmt.executeUpdate(query);
if(num==1)success=true;
stmt.close();
conn.close();
System.out.println();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
}
return success;
}
//class readingclass
public class reading {
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
writer w=new writer();
try {
tempString.getBytes("utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(!w.writ(tempString)){
System.out.println("第"+line+"行出現異常:"+tempString);
}else{
System.out.println("第"+line+"行初始化成功!");
}
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
//1927
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "D:\\BankInfo_20110714094211.csv";
readFileByLines(fileName);
}
Ⅷ java寫入文件流
簡單的方法就是先把這個HTML全部讀成一個STRING,然後正則匹配<P>標簽 然後substring截斷 插入你要插入的一串字。。。。
或者用HTMLParser也可以
http://allenj2ee.javaeye.com/blog/222454俺就是從這學的
Ⅸ Java讀寫文件的幾種方法
java讀取配置文件的幾種方法如下:
方式一:採用ServletContext讀取,讀取配置文件的realpath,然後通過文件流讀取出來。因為是用ServletContext讀取文件路徑,所以配置文件可以放入在web-info的classes目錄中,也可以在應用層級及web-info的目錄中。文件存放位置具體在eclipse工程中的表現是:可以放在src下面,也可放在web-info及webroot下面等。因為是讀取出路徑後,用文件流進行讀取的,所以可以讀取任意的配置文件包括xml和properties。缺點:不能在servlet外面應用讀取配置信息。
方式二:採用ResourceBundle類讀取配置信息,
優點是:可以以完全限定類名的方式載入資源後,直接的讀取出來,且可以在非Web應用中讀取資源文件。缺點:只能載入類classes下面的資源文件且只能讀取.properties文件。
Ⅹ java文件流怎麼寫
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
private File fileObject;
private String savePath;
FileOutputStream fileOutputStream = new FileOutputStream("savePath");
FileInputStream fileInputStream = new FileInputStream(fileObject);
byte[] buffer = new byte[100];
int len = 0;
while ((len = fileInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, len);
}