gpt4 book ai didi

java - 像 PHP 中的文件写入/读取功能

转载 作者:行者123 更新时间:2023-12-01 19:09:39 26 4
gpt4 key购买 nike

所以我每天都在 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 中的简单函数。

已编辑

所以我的功能一定是

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

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

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

最佳答案

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

关于java - 像 PHP 中的文件写入/读取功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8732674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com