電子書籍の厳選無料作品が豊富!

フォルダにあるテキストファイルを連結(2つを1つに)したいのですがどのようにプログラムすればよろしいでしょうか。
例:txt1・2・3とあれば、txt1.2、txt1.3、txt2.3みたいな感じです。

A 回答 (1件)

ファイル名:Couple.java



import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Console;
import java.util.*;
import java.io.BufferedWriter;
import java.io.FileWriter;

import java.lang.System;


public class Couple{

public static void main(String[] args){

System.out.println("ファイル連結プログラム");
System.out.print("ファイル名1:");
Console i=System.console();
String c=i.readLine();



List<String> T=new ArrayList<String>();

String str="";
try(
BufferedReader br = new BufferedReader(new FileReader(new File(c)));)
{

while((str= br.readLine()) != null){
T.add(str);

}

}catch(FileNotFoundException e){
System.out.println("ファイル名1が存在しません");
System.exit(0);
}catch(IOException e){
System.out.println("エラー");
System.exit(0);
}

System.out.print("ファイル名2:");

c=i.readLine();
str="";
try(
BufferedReader br = new BufferedReader(new FileReader(new File(c)));)
{

while((str= br.readLine()) != null){
T.add(str);

}

}catch(FileNotFoundException e){
System.out.println("ファイル名2が存在しません");
System.exit(0);
}catch(IOException e){
System.out.println("エラー");
System.exit(0);
}
System.out.print("連結ファイル名:");
c=i.readLine();

try(
BufferedWriter writer=new BufferedWriter(new FileWriter(c));){
for(String a:T){



writer.write(a);
writer.newLine();
}


}catch(Exception r){
System.out.println("連結ファイル名が存在しません");
}

}
}
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!