java - 文件 I/O - Java

标签 java file file-io

我目前正在编写一个小程序,它从一个简单文件中获取文本,并将其写入另一个文件。输入文件有点像“madlibs”类型的交易,带有我需要放置的 token ,例如“”和“”。

文字:

> One of the most <adjective> characters in fiction is named "Tarzan of the <plural-noun>." Tarzan was raised by a/an <noun> and lives in the <adjective> jungle in the heart of darkest <place>.

代码:

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

public class Story {

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

    Scanner sc = new Scanner(System.in);
    System.out.print("Input file name? ");
    String infileName = sc.next();
    Scanner input = new Scanner(new File(infileName));

    System.out.print("Output file name? ");
    String outfileName = sc.next();
    PrintStream out = new PrintStream(new File(outfileName));

    while (input.hasNext()){

        String current = input.next();
        System.out.println(current);
        int openIndex = current.indexOf("<");
        int closeIndex = current.indexOf(">");
        current = current.substring(openIndex + 1, closeIndex - openIndex);
        System.out.println(current);

        if (current.equals("adjective")){
            System.out.print("Please enter an adjective: ");
            current = sc.next();
            out.print(current);
            out.print(" ");
        } else if (current.equals("plural-noun")){
            System.out.print("Please enter a plural noun: ");
            current = sc.next();
            out.print(current);
            out.print(" ");
        } else if (current.equals("noun")){
            System.out.print("Please enter a noun: ");
            current = sc.next();
            out.print(current);
            out.print(" ");
        } else if (current.equals("place")){
            System.out.print("Please enter a place: ");
            current = sc.next(); 
            out.print(current);
            out.print(" ");
        } else {
            out.print(current);
            out.print(" ");
        }
    }
}
}

这就是我似乎遇到的问题:只有用户输入的单词才会被打印到“out1.txt”或其他内容中,并且每个单词之间有许多空格。

感谢任何帮助,谢谢各位!

最佳答案

问题来自普通单词,即不是“”:

int openIndex = current.indexOf( "<" );    // returns -1
int closeIndex = current.indexOf( ">" );   // returns -1 too
current = current.substring( openIndex + 1, closeIndex - openIndex );
// at this place for a normal word current is empty

你必须添加一些“如果”

String current = input.next();
int openIndex = current.indexOf( "<" );
if( openIndex > -1 )
{
    int closeIndex = current.indexOf( ">" );
    current = current.substring( openIndex + 1, closeIndex - openIndex );
}
System.out.println( current );

另一个问题与标记 [."] (在 [] 内)有关,您的代码仅采用 <> 和 ."内的字符。迷失了。

String current      = input.next();
String cstStrBefore = "";
String cstStrAfter  = "";
int openIndex = current.indexOf( "<" );
boolean var = ( openIndex > -1 );
if( var )
{
   int closeIndex = current.indexOf( ">" );
   cstStrBefore = current.substring( 0, openIndex );
   cstStrAfter  = current.substring( closeIndex + 1 );
   current      = current.substring( openIndex + 1, closeIndex - openIndex );
}

这是我使用的完整 Java 7 代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

public class Story {
   public static void main( String[] args ) throws FileNotFoundException {
      Scanner     sc    = new Scanner( System.in );
      Scanner     input = new Scanner( new File( "Story.txt" ));
      PrintStream out   = new PrintStream( "Story-out.txt" );
      while( input.hasNext()) {
         String current      = input.next();
         String cstStrBefore = "";
         String cstStrAfter  = "";
         int openIndex = current.indexOf( "<" );
         boolean var = ( openIndex > -1 );
         if( var ) {
            int closeIndex = current.indexOf( ">" );
            cstStrBefore = current.substring( 0, openIndex );
            cstStrAfter  = current.substring( closeIndex + 1 );
            current      =
               current.substring( openIndex + 1, closeIndex - openIndex );
         }
         System.out.println( current );
         switch( current ) {
         case "adjective"  : System.out.print( "Please enter an adjective: " ); break;
         case "plural-noun": System.out.print( "Please enter a plural noun: " ); break;
         case "noun"       : System.out.print( "Please enter a noun: " ); break;
         case "place"      : System.out.print( "Please enter a place: " ); break;
         }
         if( var ) {
            current = sc.next();
         }
         out.print( cstStrBefore + current + cstStrAfter + ' ' );
      }
      sc.close();
      input.close();
      out.close();
   }
}

建议您使用Netbeans或Eclipse进行开发。

关于java - 文件 I/O - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13162873/

相关文章:

java - 获取我在 Spring/Java/Oracle 中更新的行的有效方法

java - 我可以使用具有自定义属性名称的properties/yml 文件配置@FeignClient url 吗?

java - 无法启动引用终结器线程

c - Linux C 打开()失败

java - java socket程序中的ArrayIndexOutOfBounds异常

python - r 和 rb 模式下解析文本文件的区别

java - 实体可能不存在的 JPA 一对一关系

java - 无法删除文件,因为 JVM 持有它 - 一个棘手的问题

c++ - WAVE 文件的恐惧失败

delphi - 我们如何激活(按需)作为另一个对象属性的对象?