progressBar.set...
成都創(chuàng)新互聯(lián)專業(yè)做網(wǎng)站、成都網(wǎng)站制作,集網(wǎng)站策劃、網(wǎng)站設計、網(wǎng)站制作于一體,網(wǎng)站seo、網(wǎng)站優(yōu)化、網(wǎng)站營銷、軟文平臺等專業(yè)人才根據(jù)搜索規(guī)律編程設計,讓網(wǎng)站在運行后,在搜索中有好的表現(xiàn),專業(yè)設計制作為您帶來效益的網(wǎng)站!讓網(wǎng)站建設為您創(chuàng)造效益。
這些代碼調(diào)用以后,看能不能跟著調(diào)用界面刷新的方法,refresh,update之類(不清楚java怎么寫,我說的是C#的)
import java.io.*;
public class su {
public static void main(String args[]){
try {
BufferedReader r = new BufferedReader(new FileReader(new File("D:\\a.txt")));
PrintWriter w = new PrintWriter(new FileOutputStream(new File("D:\\b.txt")));
String line = null;
int count = 1;
while(true){
line = r.readLine();
if(line==null)break;
w.println(count+++line);
}
r.close();
w.close();
} catch (FileNotFoundException e) {
System.out.println("找不到文件或文件打開失敗");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("未知異常");
}
}
}
那個也可以呀,我這個是按我平時的習慣寫的,效果一樣的。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
*
* @author Felly
* @date 2008年12月29日16:16:12
*
*/
public class InOut {
//調(diào)試的時候文件和class文件在同目錄,不然自己加絕對路徑
public final static String inFile="in.txt";//被寫文件
public final static String outFile="out.txt";//被讀文件
public void readAndWrite(String in,String out) throws
FileNotFoundException,IOException {
File inFile=new File(in);
File outFile=new File(out);
BufferedReader reader=new BufferedReader(new FileReader(outFile));
PrintWriter pw=new PrintWriter(new FileWriter(inFile));
String line="";
int index=0;
while((line=reader.readLine())!=null)//循環(huán)到?jīng)]有內(nèi)容為止
{ index++;
line=index+line;//加上行號
pw.write(line);//寫入一行
}
//close
pw.close();
reader.close();
}
}
你好,java的API中提供了用于對象輸入輸出文件的操作,實例代碼如下:
定義單詞類如下(注意:你定義的類要實現(xiàn)Serializable接口)
public class Words implements Serializable {
private int size;
private String[] words;
public Words(){};
public Words(String...strs){
this.words = strs;
this.size = strs.length;
}
@Override
public String toString() {
return "Words{" +
"size=" + size +
", words=" + Arrays.toString(words) +
'}';
}
}
2. 對象輸入輸出api測試類
public class ObjIOTest {
public static void main(String[] args) {
String path = "d:/myIOTest.txt";
ObjIOTest objIOTest = new ObjIOTest();
Words words = new Words("hello", "my", "dear", "friend");
try {
objIOTest.writeObject(path,words);
Words wordsFromFile = (Words)objIOTest.readObject(path);
System.out.println(wordsFromFile.toString());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
//java serialize a object to file
public void writeObject(String path,Object map) throws IOException{
File f=new File(path);
FileOutputStream out=new FileOutputStream(f);
ObjectOutputStream objwrite=new ObjectOutputStream(out);
objwrite.writeObject(map);
objwrite.flush();
objwrite.close();
}
// read the object from the file
public Object readObject(String path) throws IOException, ClassNotFoundException{
FileInputStream in=new FileInputStream(path);
ObjectInputStream objread=new ObjectInputStream(in);
Object map=objread.readObject();
objread.close();
return map;
}
}
把兩段代碼拷貝到一個包下即可運行了,希望您的問題得到解答
public?class?test?{
public?static?void?main(String[]?args)?throws?IOException?{
//輸出流
FileOutputStream?fos?=?new?FileOutputStream("mytext.txt");
DataOutputStream?dos?=?new?DataOutputStream(fos);
//輸入流
FileInputStream?fis?=?new?FileInputStream("mytext.txt");
DataInputStream?dis?=?new?DataInputStream(fis);
//隨機種子
Random?random?=?new?Random();
int[]?nums?=?new?int[20];//存放20個數(shù)的數(shù)組
System.out.print("產(chǎn)生的隨機數(shù):?");
for?(int?i?=?0;?i??20;?i++)?{
nums[i]?=?random.nextInt(100);//?產(chǎn)生在0~99之間的隨機數(shù)
System.out.print(nums[i]+"?,");
}
//冒泡法排序,從小到大
for?(int?i?=?0;?i??nums.length;?i++)?{
for?(int?j?=?i;?j??nums.length;?j++)?{
if?(nums[i]??nums[j])?{
int?tmp?=?nums[i];
nums[i]?=?nums[j];
nums[j]?=?tmp;
}
}
}
//寫入文件
for?(int?i?=?0;?i??nums.length;?i++)?{
dos.writeInt(nums[i]);
dos.flush();
}
System.out.print("\n從文件中讀出數(shù)據(jù):?");
//讀出數(shù)據(jù)
for(int?i=0;i20;i++)
{
int?result?=?dis.readInt();
System.out.print("?,"+result);
}
}
}
運行結果:
希望對題主有所幫助,望采納!
代碼如下:
import?java.io.BufferedReader;
import?java.io.BufferedWriter;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileNotFoundException;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.InputStreamReader;
import?java.io.OutputStreamWriter;
public?class?App?{
public?static?void?main(String[]?args)?throws?FileNotFoundException,?IOException?{
try?(
FileInputStream?inputStream?=?new?FileInputStream(new?File("d:\\temp\\abc.txt"));
InputStreamReader?streamReader?=?new?InputStreamReader(inputStream);
BufferedReader?reader?=?new?BufferedReader(streamReader);
FileOutputStream?outputStream?=?new?FileOutputStream(new?File("d:\\temp\\output.txt"));
OutputStreamWriter?streamWriter?=?new?OutputStreamWriter(outputStream);
BufferedWriter?writer?=?new?BufferedWriter(streamWriter);
)?{
int?count?=?1;
String?line?=?null;
while?((line?=?reader.readLine())?!=?null)?{
writer.write(count++?+?":?"?+?line);
writer.newLine();
}
}
System.out.println("done.");
}
}
文章標題:JAVA順序輸入流代碼 java的輸入流
文章路徑:http://jinyejixie.com/article32/doddopc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供電子商務、商城網(wǎng)站、網(wǎng)站改版、響應式網(wǎng)站、網(wǎng)站排名、網(wǎng)頁設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)