国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Java創(chuàng)建TXT文件并進(jìn)行讀、寫、修改操作
import java.io.*;


/**
 * 
 * 功能描述:創(chuàng)建TXT文件并進(jìn)行讀、寫、修改操作
 *      

 
*/

public class ReadWriteFile {
    
public static BufferedReader bufread;
    
//指定文件路徑和名稱
    private static String path = "D:/suncity.txt";
    
private static  File filename = new File(path);
    
private static String readStr ="";


    
/**
     * 創(chuàng)建文本文件.
     * 
@throws IOException 
     * 
     
*/

    
public static void creatTxtFile() throws IOException{
        
if (!filename.exists()) {
            filename.createNewFile();
            System.err.println(filename 
+ "已創(chuàng)建!");
        }

    }

    
    
/**
     * 讀取文本文件.
     * 
     
*/

    
public static String readTxtFile(){
        String read;
        FileReader fileread;
        
try {
            fileread 
= new FileReader(filename);
            bufread 
= new BufferedReader(fileread);
            
try {
                
while ((read = bufread.readLine()) != null{
                    readStr 
= readStr + read+ "\r\n";
                }

            }
 catch (IOException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
 catch (FileNotFoundException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }


        System.out.println(
"文件內(nèi)容是:"+ "\r\n" + readStr);
        
return readStr;
    }

    
    
/**
     * 寫文件.
     * 
     
*/

    
public static void writeTxtFile(String newStr) throws IOException{
        
//先讀取原有文件內(nèi)容,然后進(jìn)行寫入操作
        String filein = newStr + "\r\n" + readStr + "\r\n";
        RandomAccessFile mm 
= null;
        
try {
            mm 
= new RandomAccessFile(filename, "rw");
            mm.writeBytes(filein);
        }
 catch (IOException e1) {
            
// TODO 自動(dòng)生成 catch 塊
            e1.printStackTrace();
        }
 finally {
            
if (mm != null{
                
try {
                    mm.close();
                }
 catch (IOException e2) {
                    
// TODO 自動(dòng)生成 catch 塊
                    e2.printStackTrace();
                }

            }

        }

    }

    
    
/**
     * 將文件中指定內(nèi)容的第一行替換為其它內(nèi)容.
     * 
     * 
@param oldStr
     *            查找內(nèi)容
     * 
@param replaceStr
     *            替換內(nèi)容
     
*/

    
public static void replaceTxtByStr(String oldStr,String replaceStr) {
        String temp 
= "";
        
try {
            File file 
= new File(path);
            FileInputStream fis 
= new FileInputStream(file);
            InputStreamReader isr 
= new InputStreamReader(fis);
            BufferedReader br 
= new BufferedReader(isr);
            StringBuffer buf 
= new StringBuffer();

            
// 保存該行前面的內(nèi)容
            for (int j = 1; (temp = br.readLine()) != null
                    
&& !temp.equals(oldStr); j++{
                buf 
= buf.append(temp);
                buf 
= buf.append(System.getProperty("line.separator"));
            }


            
// 將內(nèi)容插入
            buf = buf.append(replaceStr);

            
// 保存該行后面的內(nèi)容
            while ((temp = br.readLine()) != null{
                buf 
= buf.append(System.getProperty("line.separator"));
                buf 
= buf.append(temp);
            }


            br.close();
            FileOutputStream fos 
= new FileOutputStream(file);
            PrintWriter pw 
= new PrintWriter(fos);
            pw.write(buf.toString().toCharArray());
            pw.flush();
            pw.close();
        }
 catch (IOException e) {
            e.printStackTrace();
        }

    }

    
/**
     * main方法測(cè)試
     * 
@param s
     * 
@throws IOException
     
*/

    
public static void main(String[] s) throws IOException {
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
bootstrap-treeview demo (樹形展示與后臺(tái)數(shù)據(jù)交互)
Socket編程
java讀寫文件
java 追加寫文件
MQTT服務(wù)器系列之如何保存詳細(xì)日志信息
Java追加文件內(nèi)容的三種方法
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服