Java 解析字符串输出到文件
这个“科学怪人版”Java的第一部分工作得非常完美,但是第二部分输出了一些杂乱无章的废话。因此,结果的变量将是我从用户那里输入的。在解析之前,我必须先将字符串大写,因为一些愚蠢的原因,当你来自数据库/分析背景,知道你在几秒钟内做了一些事情而没有出错时,这很难。。。我在代码范围内给予信用 myfile.txt-->[Ljava.lang.String;@19821fJava 解析字符串输出到文件,java,android,string,split,Java,Android,String,Split,这个“科学怪人版”Java的第一部分工作得非常完美,但是第二部分输出了一些杂乱无章的废话。因此,结果的变量将是我从用户那里输入的。在解析之前,我必须先将字符串大写,因为一些愚蠢的原因,当你来自数据库/分析背景,知道你在几秒钟内做了一些事情而没有出错时,这很难。。。我在代码范围内给予信用 myfile.txt-->[Ljava.lang.String;@19821f import java.io.*; /*http://docs.oracle.com/javase/6/docs/api/jav
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");
}
}
}
}
import java.io.*;
/*http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29*/
公共类字符串分析器{
公共静态void main(字符串arg[])
抛出FileNotFoundException{
String result=“鸡蛋吐司培根培根黄油冰啤酒”。toUpperCase();
字符串[]resultU=result.split(\\s”);
字符串[]y=resultU;
{
对于(int x=0;x
您必须迭代数组并逐个写入每个元素
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");
}
您是否意识到正在为数组中的每个元素覆盖相同的文件 你应该使用
out = new FileOutputStream("myfile.txt", true); // appends to existing file
以及打印实际元素,而不是整个数组的字符串表示形式
尽管您可能应该更新代码,只创建一次输出文件,并且只将数组的每个元素写入同一个输出流,因为当前的方法效率有点低
差不多
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) {
}
}
}
你的线路
p.println (resultU);
正在打印数组本身的字符串表示形式,而不是其中的元素。要打印元素,您需要在数组中循环并单独打印出来。当然,有一种方便的方法可以为您执行此操作。即“混乱无意义”是字符串
在内存中的位置,但现在这并不重要
您的问题的解决方案是:
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
循环。我是Java新手,所以我将看看如何做到这一点。由于流在try
块中关闭,我建议也将声明放在其中。但是,您的代码几乎完全是我的。几乎没有区别:)我对Java不熟悉,所以我尝试通过putt学习把我的脚放在火上……:)谢谢……让我试试……谢谢!我以为“乱七八糟的废话”是计算机智能的东西。谢谢!
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();
}