2012-01-04 66 views
0

所以我在Java中每天都在學習新的東西,並且我希望有一天我會在Java中擁有和PHP相同的知識。像PHP一樣的文件寫入/讀取功能

我試圖做一個類,它是類似於fopenfwritefclose在PHP中,如:

<?php 
$fp = fopen('data.txt', 'w'); 
fwrite($fp, '1'); 
fwrite($fp, '23'); 
fclose($fp); 

// the content of 'data.txt' is now 123 and not 23! 
?> 

我也需要寫

Ø的方法 - 進行刪除和寫/覆蓋

a - 用於追加末尾

和一個讀取函數,它會逐行返回內容,所以我可以把它放到一個數組中,如file_get_contents(file);

這是我迄今爲止...

import java.io.*; 
import java.util.Scanner; 

/** 
Read and write a file using an explicit encoding. 
Removing the encoding from this code will simply cause the 
system's default encoding to be used instead. 
**/ 
public final class readwrite_txt 
{ 

    /** Requires two arguments - the file name, and the encoding to use. **/ 
    public static void main(String[] args) throws IOException 
    { 
     String fileName = "text.txt"; 
     String encoding = "UTF-8"; 

     readwrite_txt test = new readwrite_txt(fileName,encoding); 
     test.write("argument.txt","some text","UTF-8","o"); 
    } 

    /** Constructor. **/ 
    readwrite_txt(String fileName, String encoding) 
    { 
     String fEncoding = "text.txt"; 
     String fFileName = "UTF-8"; 
    } 

    /** Write fixed content to the given file. **/ 
    public void write(String fileName,String input,String encoding,String writeMethod) throws IOException 
    { 
     // Method overwrite 
     if(writeMethod == "o") 
     { 
      log("Writing to file named " + fileName + ". Encoding: " + encoding); 
      Writer out = new OutputStreamWriter(new FileOutputStream(fileName), encoding); 
      try 
      { 
       out.write(input); 
      } 
      finally 
      { 
       out.close(); 
      } 
     } 
    } 

    /** Read the contents of the given file. **/ 
    public void read(String fileName,String output,String encoding,String outputMethod) throws IOException 
    { 
     log("Reading from file."); 
     StringBuilder text = new StringBuilder(); 
     String NL = System.getProperty("line.separator"); 
     Scanner scanner = new Scanner(new FileInputStream(fileName), encoding); 
     try 
     { 
      while (scanner.hasNextLine()) 
      { 
      text.append(scanner.nextLine() + NL); 
      } 
     } 
     finally 
     { 
      scanner.close(); 
     } 
     log("Text read in: " + text); 
    } 

    // Why write System.out... when you can make a function like log("message"); simple! 
    private void log(String aMessage) 
    { 
    System.out.println(aMessage); 
    } 
} 

也,我不明白爲什麼我必須

readwrite_txt test = new readwrite_txt(fileName,encoding); 

,而不是

readwrite_txt test = new readwrite_txt(); 

我只是想要有一個類似於PHP的簡單函數。

EDITED

所以我的函數必須是

$fp = fopen('data.txt', 'w'); ==> readwrite_txt test = new readwrite_txt(filename,encoding,writeMethod); 

fwrite($fp, '23');   ==> test.write("the text"); 

fclose($fp);     ==> ??? 

回答

2

在Java中讀取一個文件就可以了

FileInputStream fstream = new FileInputStream("file.txt"); 
BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 
String strLine; 

while ((strLine = br.readLine()) != null) //Start of reading file 
{ 
    //what you want to do with every line is here 
} 

readwrite_txt test = new readwrite_txt();問題.. 你必須在類中的另一個構造函數不帶任何參數

+0

的作品,但我必須使它像一個功能,它可能會返回一個數組,這個想法是,一切必須是有序的,優雅... – Master345 2012-01-04 19:40:24

+1

返回,您可以使用字符串的ArrayList的數組,你可以請閱讀有關該課程的更多信息:http://javarevisited.blogspot.com/2011/05/example-of-arraylist-in-java-tutorial.html。您應該將緩衝區中的所有行添加到此ArrayList並返回ArrayList。如果你使用Java,忘記簡單的功能,所有的東西都應該是一個類的一部分。這有很多的好處,也有很多缺點。 – 2012-01-04 19:44:52

+0

所以你的問題是在返回數組? 的ArrayList將解決您的肯定 問題,這是因爲它 'ArrayList的陣列=新的ArrayList ()僞代碼; array.add(「1」); array.add(「2」); array.clear();' – mhyassin 2012-01-04 19:50:34

1

您可以使用BufferedReader,here是一個例子,但是BufferedWriter,here是寫的一個例子,here是附加的例子。對於逐行讀取,您可以使用BufferedReader的readLine方法。你不需要這些參數在你的構造函數中,因爲你不使用它們,但你甚至不需要一個類來實現這些特性,因爲已經有了用於這個目的的標準類。

我希望這會有所幫助。

+0

有趣,但有沒有辦法讓它看起來像在PHP?如果這不是一個recomanded方法做一個類看起來像在PHP中,很好,我正在讀/寫JAVA風格... – Master345 2012-01-04 19:45:02

+0

Java是一個基於類的面向對象技術。你永遠不應該明確地希望你的Java代碼看起來像一個PHP代碼,因爲這兩種技術有不同的範例。沒有必要重新發明輪子,你應該儘可能使用標準類,例外情況是他們無法做某事的情況。但即使如此,與從0寫入新類相比,擴展/實現給定的標準類仍然是一個更好的選擇。如果要使用Java,則更好地習慣Java風格,如果沒有其他代碼,則更容易理解其他代碼。 – 2012-01-04 21:38:13

+0

Java並不是那麼邪惡,因爲它可能乍看起來很像,你可以用Java做很好的事情,但是想用PHP編寫Java代碼,就像想用德語口音說英語。它可能會奏效,但你永遠處於劣勢。當你學習一種新的語言時,更好地處理你的口音。你可以寫出優秀的德語詩歌,但如果你知道我的意思,最好儘可能沒有口音說英語。 – 2012-01-04 21:42:12