-
November 26th, 2015, 05:49 PM
#16
Re: how to write Strings into a .dat
Originally Posted by Norm
Does it do what you want?
no idea.
how about this:
I create an empty String and then when I enter the FileName I use concat.
would that be fine or unnecessary complicated?
then
dos.writeChars(StringEmpty.concat(idk));
-
November 26th, 2015, 05:54 PM
#17
Re: how to write Strings into a .dat
Did that code output what you want? You should code, compile and execute different code to see what code will do what you want.
I don't know what the purpose of the empty String is.
Norm
-
November 26th, 2015, 06:02 PM
#18
Re: how to write Strings into a .dat
Originally Posted by Norm
Did that code output what you want? You should code, compile and execute different code to see what code will do what you want.
I don't know what the purpose of the empty String is.
I dont know how to test it. also, it wasnt for entering via keyboard
-
November 26th, 2015, 06:39 PM
#19
Re: how to write Strings into a .dat
I dont know how to test it
Testing code is very important. You need to figure out a way to test the code so that you know if it is working correctly or not.
Sometimes you need to code sample data into the program for testing methods and other bits of code in place of getting the data from the source the finished program will use.
I posted sample code for testing the DataOutputStream class's methods in post#11
Norm
-
November 27th, 2015, 06:51 PM
#20
Re: how to write Strings into a .dat
"Streams (InputStream and OutputStream) transfer binary data. If you want to write a string to a stream, you must first convert it to bytes, or in other words encode it. You can do that manually (as you suggest) using the String.getBytes(Charset) method, but you should avoid the String.getBytes() method, because that uses the default encoding of the JVM, which can't be reliably predicted in a portable way.
The usual way to write character data to a stream, though, is to wrap the stream in a Writer, (often a PrintWriter), that does the conversion for you when you call its write(String) (or print(String)) method. The corresponding wrapper for InputStreams is a Reader.
PrintStream is a special OutputStream implementation in the sense that it also contain methods that automatically encode strings (it uses a writer internally). But it is still a stream. You can safely wrap your stream with a writer no matter if it is a PrintStream or some other stream implementation. There is no danger of double encoding."
how do I transform String into Bytes? (wrap the stream in a Writer).
-
November 27th, 2015, 07:26 PM
#21
Re: how to write Strings into a .dat
Look at the methods of the DataOutputStream. I think one of them will do it.
Norm
-
November 28th, 2015, 10:21 AM
#22
Re: how to write Strings into a .dat
The method Write(byte[]) is undefined for the type DataOutputStream
Code:
System.out.print("Enter Filename:"); //path
s = sc.nextLine();
byte[] bytes = s.getBytes();
dos.WriteBytes(bytes);
how do I fix that error?
public final void writeBytes(String s)
throws IOException
Writes out the string to the underlying output stream as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits. If no exception is thrown, the counter written is incremented by the length of s.
Specified by:
writeBytes in interface DataOutput
Parameters:
s - a string of bytes to be written.
Throws:
IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out
-
November 28th, 2015, 10:31 AM
#23
Re: how to write Strings into a .dat
method Write(byte[]) is undefined
Java is case sensitive: W is different from w
Make sure the case is correct.
Norm
-
November 28th, 2015, 10:42 AM
#24
Re: how to write Strings into a .dat
Originally Posted by Norm
Java is case sensitive: W is different from w
Make sure the case is correct.
Now I cant even enter the Filename it goes "Enter Filename:Enter the amount of customized fields:"
Code:
System.out.print("Enter Filename:"); //path
s = sc.nextLine();
dos.writeBytes(s);
//Date.valueOf(LocalDate.of(2, 2, 2));// date here
// dos.writeInt(d);
System.out.print("Enter the amount of customized fields:"); //campos custom
t = sc.nextInt();
-
November 28th, 2015, 10:48 AM
#25
Re: how to write Strings into a .dat
What is the value of s that is read by nextLine()? Does it have the expected value or is it empty?
It may be a problem with the Scanner class's methods that leave the endline char in its buffer.
Norm
-
November 28th, 2015, 11:00 AM
#26
Re: how to write Strings into a .dat
Originally Posted by Norm
What is the value of s that is read by nextLine()? Does it have the expected value or is it empty?
It may be a problem with the Scanner class's methods that leave the endline char in its buffer.
"filename" is just a string (exmpl: asdasdasdaf)
-
November 28th, 2015, 11:19 AM
#27
Re: how to write Strings into a .dat
Yes, that is what you are expecting.
I'm asking: What is the value of s when the code is executed? Add a print statement after where s is read that prints the value of s.
Norm
-
November 28th, 2015, 11:50 AM
#28
Re: how to write Strings into a .dat
Originally Posted by Norm
Yes, that is what you are expecting.
I'm asking: What is the value of s when the code is executed? Add a print statement after where s is read that prints the value of s.
oohh. I've set it as String s; but is not highlighted. and "s is never used".
-
November 28th, 2015, 11:54 AM
#29
Re: how to write Strings into a .dat
I don't understand what that means. I clearly see that s is used on these lines:
Code:
s = sc.nextLine();
dos.writeBytes(s);
Norm
-
November 28th, 2015, 12:01 PM
#30
Re: how to write Strings into a .dat
Originally Posted by Norm
I don't understand what that means. I clearly see that s is used on these lines:
Code:
s = sc.nextLine();
dos.writeBytes(s);
see? var "s" was never given a type.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|