こんにちは
掲題の通りなのですが、java.ioを利用し、
①ファイルを読み込む。
②ファイルの中を一行ずつ読み込み、「男」という文字がある行のみ削除。
というメソッドを作成しています。
①は完了していますので、
・ファイルの変数を受取り、②の作業を完了したファイルを返すメソッド
を作成したいと考えています。
どのようなメソッドが考えられますか?
どなたかご教授ください。
-----以下、書きかけのソースコード---------------------------------------------
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.nio.channels.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
class FileUtils {
public static void main(String args[]) { String inName = "in"; String outName = "out"; String name = System.getProperty("user.name"); File inFile = new File("C:\\Users\\" + name + "\\Documents\\NetBeansProjects\\amv-repo\\kako-aws-rest-dto\\src\\main\\java\\jp\\co\\sjnk\\kako\\aws\\dto\\customer\\" + inName + ".txt"); File outFile = new File("C:\\Users\\" + name + "\\Documents\\NetBeansProjects\\amv-repo\\kako-aws-rest-dto\\src\\main\\java\\jp\\co\\sjnk\\kako\\aws\\dto\\customer\\" + outName + ".txt"); try { FileUtils.copyFile(inFile, outFile); FileUtils.deleteAnnotations(outFile); } catch (IOException e) { e.printStackTrace(); } } public static void copyFile(File in, File out) throws IOException { FileChannel inChannel = new FileInputStream(in).getChannel(); FileChannel outChannel = new FileOutputStream(out).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } catch (IOException e) { throw e; } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } } public static void deleteAnnotations(File out) throws IOException { //TODO 「男」を含む行の削除 }
}
回答1件
あなたの回答
tips
プレビュー