2016-04-23 24 views
0

コードに1行のテキストを入力して、そのすべてのテキストをファイルに出力する必要があります。私は文字列でこれを行う方法を知っていますが、この方法でファイルに印刷する方法を理解できません。私は、実際にPrintToFile.txtにこれらのすべての方法を実際に入力することなく、それらを出力するようにテキストを出力することに助けは必要ありません。文字列をすべて大文字、小文字、逆Javaでファイルに出力

import java.io.FileNotFoundException; 
import java.lang.SecurityException; 
import java.util.*; 
import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 

public class PrintToFile { //open class 

private static Formatter output; 

public static void main (String args[]) throws IOException { //open main  
openFile(); 
addRecords(); 
closeFile(); 

BufferedReader printFile = new BufferedReader(new FileReader("YoastReginaITM251Project9.txt")); 

for (String line; (line = printFile.readLine()) != null;) { //open for 
    System.out.println("Text: " + line); 
    System.out.println("Text in Upper Case: " + line.toUpperCase()); 
    System.out.println("Text in Lower Case: " + line.toLowerCase()); 
    System.out.println("Text in Reverse Case: " + line); 
} //close for 

} //close main 

public static void openFile() { //open openFile 

try { //open try 
    output = new Formatter("PrintToFile.txt"); //open file 
} //close try 

catch (SecurityException securityException) { //open catch 
    System.err.println("Write permission denied. Terminating."); 
    System.exit(1); 
} //close catch 

catch (FileNotFoundException fileNotFoundException) { //open catch 
    System.err.println("Error opening file. Terminating."); 
    System.exit(1); 
} //close catch 

} //close openFile 

public static void addRecords() { //open addRecords 


    try { //open try 
    output.format("%s", input.nextLine()); 
    } //close try 
    catch (FormatterClosedException formatterClosedException) { //open catch 
    System.err.println("Error writing to file. Terminating."); 
    } //close catch 
    catch (NoSuchElementException elementExpcetion) { //open catch 
    System.err.println("Invalid input. Please try again."); 
    input.nextLine(); 
    } //close catch 


    } //close AddRecords 

public static void closeFile() { //open closeFile 

    if (output != null) 
    output.close(); 

    } //close closeFile 

} //close class 
+0

'FileWriter'を使用して、文字化けした文字列をファイルに出力できますか? – KevinO

答えて

0

あなたは、ファイルにそれらを印刷し、あなたが好きなように新しい文字列を作成するために、StringBuilderクラス(https://docs.oracle.com/javase/tutorial/java/data/buffers.html)を使用することができます。

+0

ありがとう!それはどうしたらいいですか?私はそれを私のAddRecordsや他のどこかにプリントするだろうか?私はStringBuilderにはあまり経験がありませんし、少し難しいコードについても読んでいます。 – aymequinn

+0

あなたの目標が完全にはっきりしていません。しかし、これはあなたが達成しようとしているもののように見えますか?:http://stackoverflow.com/questions/1677194/dumping-a-java-stringbuilder-to-file –

1
String input = "MagicString"; 

    String upperCase = input.toUpperCase(); 
    String lowerCase = input.toLowerCase(); 

    StringBuilder sb = new StringBuilder(); 
    sb.append(input); 

    String reversedString = sb.reverse().toString(); 
0

簡単な方法は次のとおりです。文字列にファイルから

  1. 読みます。
  2. toUpperCase()を適用し、別の文字列に格納します。
  3. toLowerCase()に適用し、別の文字列に格納します。
  4. reverse()[own created method]を適用し、別の文字列に格納します。
  5. これらすべての文字列を宛先ファイルに書き込みます。

ここでは、必要な操作を実行するためのコードを示します。

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
public class FileRW { 

public static void main(String[] args) throws FileNotFoundException, IOException { 

String filename="filename.txt"; 
    String upper,lower,reverse,line; 
    upper=null; 
    lower=null; 
    reverse=null; 
    line=null; 

    FileReader fileReader=new FileReader(filename); 

    BufferedReader bufferedReader=new BufferedReader(fileReader); 

    while((line=bufferedReader.readLine())!=null) 
    { 
    upper=line.toUpperCase(); 

    lower=line.toLowerCase(); 

    reverse=reverse(line); 

    writeToFile(upper,lower,reverse); 
    } 
} 
static String reverse(String test) 
{ String returnString=""; 

    int len=test.length(); 

    for(int i=len-1;i>=0;i--) 
    { 
     returnString+=test.charAt(i); 
    } 
    return returnString; 
} 

static void writeToFile(String line1,String line2,String line3) throws IOException 
{ 
    String filename="content.txt"; 


     File file =new File(filename); 

     //if file doesnt exists, then create it 
     if(!file.exists()){ 
      file.createNewFile(); 
     } 

     //true = append file 
     FileWriter fileWritter = new FileWriter(file.getName(),true); 
      BufferedWriter bufferWritter = new BufferedWriter(fileWritter); 
      bufferWritter.write(line1); 
      bufferWritter.write(line3); 
      bufferWritter.write(line2); 
      bufferWritter.close(); 

     System.out.println("Done"); 
    } 
} 
0

多分、ScannerクラスとFileWriterクラスを使用できます。

import java.io.*; 
import java.util.*; 

public class Solution{ 

    /* 
    * Print a string to the output file 
    */ 

    private void print(String s, FileWriter o) { 

     try { 

      o.write(s + "\n"); 

     } catch (IOException e) { 

      e.printStackTrace(); 

     } 

    } 


    /* 
    * Convert the strings to uppercase, 
    * lowercase and reverse. 
    */ 

    private void solver(Scanner sc, FileWriter o){ 

     while(sc.hasNextLine()) { 

      String s = sc.nextLine(); 
      print(s.toUpperCase(), o); 
      print(s.toLowerCase(), o); 
      print(new StringBuilder(s).reverse().toString(), o); 

     } 

    } 


    /* 
    * Main method 
    */ 

    public static void main(String args[]){ 

     File inFile = new File("input.txt"); 
     File outFile = new File("output.txt"); 

     try{ 

      Scanner sc = new Scanner(inFile); 
      FileWriter o = new FileWriter(outFile); 
      Solution s = new Solution(); 
      s.solver(sc, o); 
      sc.close(); 
      o.close(); 

     } catch(Exception e){ 

      System.out.println(e); 

     } 
    } 
} 
関連する問題