在现有文件中读写 [英] read and write in existing file

查看:63
本文介绍了在现有文件中读写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 that dir will not be initialised to an existing directory. Then your files variable will be NULL:

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 following for loop f will become NULL too and the next command f.isFile() (in line 15) will throw the exception.


这篇关于在现有文件中读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆