像PHP一样的文件写/读功能

时间:2012-01-04 19:19:34

标签: java file-io

所以我在Java中日复一日地学习新东西,我希望有一天我应该在Java中拥有与PHP相同的知识。

我正在尝试在PHP中创建类似于fopenfwritefclose的类,如:

<?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!
?>

我还需要写作方法

o - 用于删除和写入/覆盖

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);                  ==> ???

3 个答案:

答案 0 :(得分: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();问题.. 你必须在类中有另一个不带任何参数的构造函数

答案 1 :(得分:1)

查看以下文件处理教程(谷歌充斥着它们):

注意以下几个类:

  • 的FileInputStream
  • FileOutpuStream
  • 扫描仪

有各种各样的例子供你学习。

答案 2 :(得分:1)

你可以使用BufferedReader,here是一个例子,BufferedWriter,here是写的一个例子,here是一个附加的例子。对于逐行读取,您可以使用BufferedReader的readLine方法。您不需要在构造函数中使用这些参数,因为您不使用它们,但您甚至不需要类来实现这些功能,因为已经有用于此目的的标准类。

我希望这会有所帮助。