ดู java อ่านเขียนไฟล์ ช่วยผมหน่อยครับ

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

วัชระ สินธุมาลา
PHP Newbie
PHP Newbie
โพสต์: 2
ลงทะเบียนเมื่อ: 02/11/2019 3:09 pm

ดู java อ่านเขียนไฟล์ ช่วยผมหน่อยครับ

โพสต์ที่ยังไม่ได้อ่าน โดย วัชระ สินธุมาลา »

ผมต้องการค่าของ ตัวแปรอะเร ใน เมทธอด ของคลาสอื่น มาใช้งาน มันไม่ยอมมา

โค้ด: เลือกทั้งหมด

import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Write3 { 

   public static void modifyFile(String oldString, String newString){//method edit mac
        //1
        String originalFilePath = "d:/java_shutdown/dhcpd.conf";
        String originalFileContent = "";
        //2
        BufferedReader reader = null;
        BufferedWriter writer = null;
        //3
        try {
            //4
            reader = new BufferedReader(new FileReader(originalFilePath));
            //5
            String currentReadingLine = reader.readLine();
            //6
            while (currentReadingLine != null) {
                originalFileContent += currentReadingLine + System.lineSeparator();
                currentReadingLine = reader.readLine();
            }
            //7
            String modifiedFileContent = originalFileContent.replaceAll(oldString, newString);
            
            //8
            writer = new BufferedWriter(new FileWriter(originalFilePath));
            //9
            writer.write(modifiedFileContent);
        } catch (IOException e) {
            //handle exception
        } finally {
            //10
            try {
                if (reader != null) {
                    reader.close();
                }
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                //handle exception
            }
        }
    }////End method  modifyFile
     public static void ReadFile() {
        String [] getHost = null, preCutMac = null; 
        String [] hostNameClient = new String [13];
        String [] MacClient = new String [13];
        String cutMac = null, SendVariableMac = null;
        File f = new File("d:/java_shutdown/dhcpd.conf");
        int count = 0 ; 
        FileInputStream inputStream = null;
       try {
           inputStream = new FileInputStream(f);
       } catch (FileNotFoundException ex) {
           Logger.getLogger(Write3.class.getName()).log(Level.SEVERE, null, ex);
       }
        Scanner sc = new Scanner(inputStream, "UTF-8");
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            if(line.contains("host client") ){   
                count++;
                getHost = line.split(" ");
                hostNameClient [count] = getHost[3];
            }
           if(line.contains("hardware ethernet")){//getmac
                preCutMac = line.split(" ");
                for (int i=0; i < preCutMac.length; i++) {
                SendVariableMac = preCutMac[6];
                }
            cutMac = SendVariableMac.substring(0, 17);
            MacClient [count] = cutMac;
            }
        }//end loop while 
         for(int i=1; i < hostNameClient.length; i++   ){ 
             if(i==3){
             }
            System.out.print(hostNameClient[i]);
            System.out.print(" --> ");
            System.out.println(MacClient[i]);
        }    
    }//End method ReadFile
    
}//End Class Writ3
class Test { 
    
    public static void main(String args[])throws FileNotFoundException, IOException {
        Write3 w3 = new Write3();
     int b;
       // b = w3.hostNameClient[].length;
       b=Write3.ReadFile().hostNameClient.length;
         System.out.print(hostNameClient[i]);
            System.out.print(" --> ");
            System.out.println(MacClient[i]);
    }
}
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 92