2011-12-13 82 views
0

该“Frankenstein-ed”Java的第一部分完美工作,但第二部分输出一些混乱的废话。所以结果的变量将是我的用户输入。由于某种愚蠢的原因,我必须首先处理UpperCase字符串,但是当你来自数据库/分析背景并且知道你在几秒钟内做了某些事情并且没有发现错误时,很难。我在信用证到期时给予了信用代码...解析字符串输出到文件

myfile.txt的---> [Ljava.lang.String内; @ 19821f

import java.io.*; 
/*http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29*/ 

    public class StringParser { 

    public static void main (String arg[]) 
    throws FileNotFoundException { 
String result = "eggs toast bacon bacon butter ice beer".toUpperCase(); 
    String[] resultU = result.split("\\s"); 
    String[] y = resultU; 

    { 
for (int x=0; x< resultU.length; x++) 



    System.out.println(resultU[x]); 


    /*http://www.javacoffeebreak.com/java103/java103.html#output*/ 

      FileOutputStream out; // declare a file output object 
      PrintStream p; // declare a print stream object 

      try 
      { 
        // Create a new file output stream 
        // connected to "myfile.txt" 
        out = new FileOutputStream("myfile.txt"); 

        // Connect print stream to the output stream 
        p = new PrintStream(out); 

        p.println (resultU); 

        p.close(); 
      } 
      catch (Exception e) 
      { 
        System.err.println ("Error writing to file"); 
      } 
    } 
} 
} 

回答

2

您是否意识到您正在覆盖阵列中每个元素的相同文件?

您应该使用

out = new FileOutputStream("myfile.txt", true); // appends to existing file 

以及印刷的实际元素,而不是整个阵列

p.println(resultU[x]); // resultU without index prints the whole array - yuk! 

的字符串表示虽然你或许应该更新代码,仅创建了输出文件一次,只是将数组的每个元素写入相同的输出流,因为当前的方法效率不高。

喜欢的东西

public static void main(String[] args) { 

    String result = "eggs toast bacon bacon butter ice beer".toUpperCase(); 

    PrintStream p = null; 

    try { 
     p = new PrintStream(new FileOutputStream("myfile.txt")); 

     for (String s : result.split("\\s")) { 
      p.println(s); 
      p.flush(); // probably not necessary 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); // should really use a logger instead! 
    } finally { 
     try { 
      p.close(); // wouldn't need this in Java 7! 
     } catch (Exception e) { 
     } 
    } 
} 
+0

我是新来的Java,所以我试图通过把我的脚去火学习... :)谢谢..让我试试看... – Darren

+0

很感谢H.D.! – Darren

+0

@Darren你应该选择这个作为正确答案然后;) –

0

你必须遍历阵列和一个后一个写每个元素。

FileOutputStream out; // declare a file output object 
    PrintStream p; // declare a print stream object 
    try 
    { 
    out = new FileOutputStream("myfile.txt"); 
    p = new PrintStream(out); 
    for(String str:resultU) 
    { 
     p.println (str); 
    } 
    p.close(); 
    } 
    catch (Exception e) 
    { 
    System.err.println ("Error writing to file"); 
    } 
+0

我是新来的Java,所以我会看看如何做到这一点。 – Darren

+0

由于流正在'try'块中关闭,我建议将声明放在它的内部。但是,那么你的代码几乎就是我的。几乎没有区别:) – Jon

0

你的线

p.println (resultU); 

正在打印数组本身,而不是在它的元素的字符串表示。要打印元素,您需要遍历数组并逐个打印出来。当然,The Arrays class有一个方便的方法可以为你做到这一点。

0

“混乱的无意义”是String在记忆中的位置,但现在并不重要。

的解决问题的方法是这样的:

try { 
    FileOutputStream out = new FileOutputStream("myfile.txt", true); 
    PrintStream = new PrintStream(out); 

    for(String s : resultU) 
    p.println(s); 

    p.close(); 
} catch(Exception e) { 
    e.printStackTrace(); 
} 

这将替换整个for循环。

+0

谢谢!我认为“混乱的废话”是计算机方面的东西。谢谢! – Darren