CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 47
  1. #16
    Join Date
    Nov 2015
    Posts
    37

    Re: how to write Strings into a .dat

    Quote Originally Posted by Norm View Post
    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));

  2. #17
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  3. #18
    Join Date
    Nov 2015
    Posts
    37

    Re: how to write Strings into a .dat

    Quote Originally Posted by Norm View Post
    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

  4. #19
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  5. #20
    Join Date
    Nov 2015
    Posts
    37

    Question 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).

  6. #21
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: how to write Strings into a .dat

    Look at the methods of the DataOutputStream. I think one of them will do it.
    Norm

  7. #22
    Join Date
    Nov 2015
    Posts
    37

    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

  8. #23
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  9. #24
    Join Date
    Nov 2015
    Posts
    37

    Re: how to write Strings into a .dat

    Quote Originally Posted by Norm View Post
    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();

  10. #25
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  11. #26
    Join Date
    Nov 2015
    Posts
    37

    Re: how to write Strings into a .dat

    Quote Originally Posted by Norm View Post
    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)

  12. #27
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  13. #28
    Join Date
    Nov 2015
    Posts
    37

    Re: how to write Strings into a .dat

    Quote Originally Posted by Norm View Post
    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".

  14. #29
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: how to write Strings into a .dat

    "s is never used"
    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

  15. #30
    Join Date
    Nov 2015
    Posts
    37

    Re: how to write Strings into a .dat

    Quote Originally Posted by Norm View Post
    I don't understand what that means. I clearly see that s is used on these lines:
    Code:
        s = sc.nextLine();
        dos.writeBytes(s);
    Name:  Sin título.jpg
Views: 33
Size:  42.9 KB

    see? var "s" was never given a type.

Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured