我正在尝试编写一个代码来管理写入和读取的文件。Eclipse 通过 .readLine() 方法进行攻击。我很难理解为什么以及如何解决它。这一定是一些心理障碍,因为我已经阅读了 Oracles 网站上的教程文档,但这并没有帮助。任何人都能够将它变成这个新手可能会得到的词吗?
import java.io.*;
public class DataBase {
public static String getExtension(File f) {
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
ext = s.substring(i + 1).toLowerCase();
}
return ext;
}
public static String readFile(String fileName) {
String strData = "";
if (!new java.io.File(fileName).exists()) {
return strData;
}
File file = new File(fileName);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
StringBuffer buff = new StringBuffer();
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
String line = "";
while (dis.available() != 0) {
line = dis.readLine();
if (line.length() > 0) {
if (line.contains("<br/>")) {
line = line.replaceAll("<br/>", " ");
String tempLine = "";
while((tempLine.trim().length()<1) && dis.available()!=0){
tempLine = dis.readLine();
}
line = line + tempLine;
}
line = line.replaceAll("\"", "");
buff.append(line + "\n");
}
}
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buff.toString();
}
public static boolean writeToFile(String fileName, String data, boolean append) {
boolean isWrite = false;
int dirIndex = fileName.lastIndexOf(getPathSeparator());
if (dirIndex != -1) {
String dir = fileName.substring(0, dirIndex) + getPathSeparator();
java.io.File fDir = new java.io.File(dir);
if (!fDir.exists()) {
if (!fDir.mkdirs()) {
return false;
}
}
}
try {
java.io.FileOutputStream fout = new java.io.FileOutputStream(fileName, append);
java.nio.channels.FileChannel fChannelWriter = fout.getChannel();
byte[] bytesToWrite = data.getBytes();
java.nio.ByteBuffer bBuffW = java.nio.ByteBuffer.wrap(bytesToWrite);
fChannelWriter.write(bBuffW);
fChannelWriter.close();
fout.close();
isWrite = true;
} catch (java.io.IOException ex) {
isWrite = false;
}
return isWrite;
}
public static String getPathSeparator() {
return java.io.File.separator;
}
}