Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert char to a complete String

Status
Not open for further replies.

Kruzer

Programmer
Jun 16, 2000
117
US
I'm having a brain fart right now...

How do I take a series of chars and convert it to a complete string = "hello"

I have a loop where I'm able to get the individual char values = h,e,l,l,o

FileInputStream in = new FileInputStream(file);
int fileBuffer = 0;
char text = 0;
String completeString = "";

for(int i=0;i < 5; i++)
{
fileBuffer = in.read(); // found hello in file
text = (char)fileBuffer;
System.out.println(text) // *looping = h,e,l,l,o

//What do I do next to build completeString?
}

System.out.println(completeString) // hello



Dano
dan_kryzer@hotmail.com
What's your major malfunction
 
File f = new File(&quot;C:\\test.txt&quot;);
FileReader fr = new FileReader(f);
char[] chars = new char[(int)f.length()];
fis.read(chars, 0, chars.length);

// If you need it into a String ...
StringBuffer sb = new StringBuffer(chars.length);
for (int i = 0; i < chars.length; i++) {
sb.append(chars);
}

String s = sb.toString();
 
You can store you char on an char array and invoke the String constructor with that array.

char[] charArray;
String yourString = new String(yourCharArray);

Cheers.

Dian
 
Ha ! Or you could do that ! Nice Dian ...
 
I just tried out the &quot;loop the char[]&quot; vs &quot;new String(char[])&quot; method, and it turns out that on a 50Mb file, Dian's way is twice as quick !

public class Test {

public static void main(String args[]) throws Exception {
long start = System.currentTimeMillis();
File f = new File(&quot;C:/test2.txt&quot;);
FileReader fr = new FileReader(f);
char[] chars = new char[(int)f.length()];
fr.read(chars);


// method 1
//StringBuffer sb = new StringBuffer(chars.length);
//for (int i = 0; i < chars.length; i++) {
// sb.append(chars);
//}
//String s = sb.toString();

// method 2
//String s = new String(chars);


System.err.println(System.currentTimeMillis() - start);
}

}
 
You should think of BufferedReader instead.
Why always taking one character, if you need the whole thing?
Do you need lines or words?

br = new BufferedReader (new FileReader (file));
try
{
line = br.readLine ();
System.out.println (&quot;Test: line = &quot; + line);
}

If you need words, StringTokenizer could be a good improvement.
 
stefanwagner :

Using a BufferedReader is a seriously bad performance hit, especially if you start messing about with StringTokenizer's.

Remember that this guy wants the file to be into one String. Now the quickest way to do that is a combined method of Dian's and mine, which reads the file into a char[], and then creates a String from that char[]. On a 3Mb file, that way is done in 200 millis, with your way it is 300 millis. On a 20Mb file, the first way is 1500 millis, the buffered way is 3000 millis.

So, unless you have hardly any memory at all, the first way is much much quicker that your way.

Code:
public class Test {


	public static void main(String args[]) throws Exception {
        long start = System.currentTimeMillis();

        // method 1 - quickest
        //File f = new File(&quot;C:/test.txt&quot;);
        //FileReader fr = new FileReader(f);
        //char[] chars = new char[(int)f.length()];
        //fr.read(chars);
        //String s = new String(chars);

        // method 2 - mdedium
        //File f = new File(&quot;C:/test.txt&quot;);
        //FileReader fr = new FileReader(f);
        //char[] chars = new char[(int)f.length()];
        //fr.read(chars);
        //StringBuffer sb = new StringBuffer(chars.length);
        //for (int j = 0; j < chars.length; j++) {
        //    sb.append(chars[j]);
        //}
        //String s = sb.toString();

		// method 3 - slowest
		//String line = &quot;&quot;;
		//StringBuffer sb = new StringBuffer();
		//BufferedReader br = new BufferedReader (new FileReader (&quot;C:/test.txt&quot;));
		//while ((line = br.readLine()) != null) {
		//	sb.append(line);
		//}
		//String s = sb.toString();


        System.err.println(System.currentTimeMillis() - start);

	}

}
 
From my point of view, if you really want to have a good performance, I'd try to think on a different way of doing whatever you want to do with the content of the file instead of instantiating a 50.000.000-sized String.

I must recognize that I'm not specially concerned about performance, but I don't think Strings are designed to store such a big amount of information.

Cheers.

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top