从一个文本文件中获取数据并将其移至新的文本文件 [英] Taking data from one text file and moving it to a new text file

查看:97
本文介绍了从一个文本文件中获取数据并将其移至新的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,里面有数据.在我的主要方法中,我读取了文件并关闭了文件.我调用另一个在原始文件的同一文件夹内创建新文件的方法.所以现在我有两个文件,原始文件和从我调用的方法生成的文件.我需要另一种方法,该方法将从原始文件中获取数据并将其写入创建的新文件中.我该怎么办?

I have a file that has data inside of it. In my main method I read in the file and closed the file. I call another method that created a new file inside of the same folder of the original file. So now I have two files, the original file and the file that is being made from the method that I call. I need another method that takes the data from the original file and writes it to the new file that is created. How do I do that?

import java.io.*;
import java.util.Scanner;
import java.util.*;
import java.lang.*;

public class alice {

    public static void main(String[] args) throws FileNotFoundException {
        String filename = ("/Users/DAndre/Desktop/Alice/wonder1.txt");
        File textFile = new File(filename);
        Scanner in = new Scanner(textFile);
        in.close();
        newFile();
    }

    public static void newFile() {
        final Formatter x;
        try {
            x = new Formatter("/Users/DAndre/Desktop/Alice/new1.text");
            System.out.println("you created a new file");
        } catch (Exception e) {
            System.out.println("Did not work");
        }
    }

    private static void newData() {
    }

}

推荐答案

如果您的要求是将原始文件内容复制到新文件中.那么这可能是一个解决方案.

If your requirement is to copy your original files content to new file. Then this may be a solution.

解决方案:

首先,使用BufferedReader读取原始文件,然后将内容传递给使用PrintWriter创建新文件的另一种方法.并将您的内容添加到新文件中.

First, read to your original file using BufferedReader and pass your content to another method which creates new file using PrintWriter. and add your content to your new file.

示例:

public class CopyFile {

  public static void main(String[] args) throws FileNotFoundException, IOException {
    String fileName = ("C:\\Users\\yubaraj\\Desktop\\wonder1.txt");
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    try {
        StringBuilder stringBuilder = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            stringBuilder.append(line);
            stringBuilder.append("\n");
            line = br.readLine();
        }
        /**
         * Pass original file content as string to another method which
         * creates new file with same content.
         */
        newFile(stringBuilder.toString());
    } finally {
        br.close();
    }

  }

  public static void newFile(String fileContent) {
    try {
        String newFileLocation = "C:\\Users\\yubaraj\\Desktop\\new1.txt";
        PrintWriter writer = new PrintWriter(newFileLocation);
        writer.write(fileContent);//Writes original file content into new file
        writer.close();
        System.out.println("File Created");
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

这篇关于从一个文本文件中获取数据并将其移至新的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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