在现有文件中读写 [英] read and write in existing file
问题描述
import java.io. *;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
public class tryy
{
public static void main(字符串 args []) throws IOException
{
文件文件=新文件( D:\\fold \\myfile.txt);
PrintWriter writer = new PrintWriter(file, UTF-8\" 跨度>);
字符串 p = fold跨度>;
文件目录=新文件(p);
File [] files = dir.listFiles();
for (文件f:文件)
if (f.isFile() )
{
StringBuilder sb = new StringBuilder();
BufferedReader ins = null;
ins = new BufferedReader( new FileReader(f));
String 行;
writer.print( [);
writer.print(f.getName());
writer.println( _ DINAMALAR_DATE] \ n);
while ((line = ins.readLine())!= null)
{
writer.println(line);
writer.println(sb.toString());
System.out.println(line);
}
}
writer.close();
字符串 inputFile = D:\ \fold\\myfile.txt跨度>;
String fle = inputFile;
BufferedReader br = new BufferedReader( new FileReader(inputFile));
String line = null;
StringBuilder sb = new StringBuilder();
int count = 1;
{
while ((line = br.readLine())!= null){
if (line.startsWith( [)){
if (sb.length()!= 0){
文件file1 = new 文件(第+行+ 。txt);
PrintWriter writer1 = new PrintWriter(file1, UTF-8跨度>);
writer1.println(line);
writer1.println(sb.toString());
writer1.close();
sb.delete( 0 ,sb.length());
count ++;
}
继续;
}
sb.append(line);
}
}
}
}
我不喜欢不懂Java但是你应该检查这些行:
String p = fold;
文件目录=新文件(p);
File [] files = dir.listFiles();
for (文件f:文件)
if (f.isFile() )
根据你的评论,execption被抛到最后一行:ryy.main(tryy.java:15)
传递的路径fold看起来很可疑,我认为dir
不会初始化为现有目录。那么你的文件
变量将是NULL
:public File [] listFiles()
返回一个抽象路径名数组,表示此抽象路径名表示的目录中的文件。
如果此抽象路径名不表示目录,然后此方法返回null。
在以下中
循环f
也将变为NULL
,下一个命令f.isFile()
(第15行)将抛出异常
import java.io.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
public class tryy
{
public static void main(String args[])throws IOException
{
File file=new File("D:\\fold\\myfile.txt");
PrintWriter writer= new PrintWriter(file, "UTF-8");
String p="fold";
File dir=new File(p);
File[] files=dir.listFiles();
for(File f : files)
if(f.isFile())
{
StringBuilder sb=new StringBuilder();
BufferedReader ins=null;
ins=new BufferedReader(new FileReader(f));
String line;
writer.print("[");
writer.print(f.getName());
writer.println("_DINAMALAR_DATE]\n");
while((line=ins.readLine())!=null)
{
writer.println(line);
writer.println(sb.toString());
System.out.println(line);
}
}
writer.close();
String inputFile="D:\\fold\\myfile.txt";
String fle=inputFile;
BufferedReader br = new BufferedReader(new FileReader(inputFile));
String line=null;
StringBuilder sb = new StringBuilder();
int count=1;
{
while((line = br.readLine())!= null){
if(line.startsWith("[")){
if(sb.length()!=0){
File file1= new File(line+".txt");
PrintWriter writer1 = new PrintWriter(file1, "UTF-8");
writer1.println(line);
writer1.println(sb.toString());
writer1.close();
sb.delete(0, sb.length());
count++;
}
continue;
}
sb.append(line);
}
}
}
}
I don't know Java but you should inspect these lines:
String p="fold"; File dir=new File(p); File[] files=dir.listFiles(); for(File f : files) if(f.isFile())
According to your comment, the execption is thrown in the last line: ryy.main(tryy.java:15)
The passed path "fold" looks suspect and I assume thatdir
will not be initialised to an existing directory. Then yourfiles
variable will beNULL
:public File[] listFiles()
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
If this abstract pathname does not denote a directory, then this method returns null.
In the followingfor
loopf
will becomeNULL
too and the next commandf.isFile()
(in line 15) will throw the exception.
这篇关于在现有文件中读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!