Java not getting in expected way


 
Thread Tools Search this Thread
Top Forums Programming Java not getting in expected way
# 1  
Old 05-25-2018
Java not getting in expected way

Hi

i am new to java and written a below code on linux platform which is working fine but not getting the output on terminal as expected
Code:
 
 vi A.java 
import java.io.FileOutputStream;
class A
{
public static void main (String args[])
{
byte[] c= {'a','e','i','o','u'};
try
{
FileOutputStream b =new FileOutputStream("/home/k");
b.write(65);
b.write(c,1,4);
b.getChannel();
b.close();
System.out.println("success");
System.out.println(b.getChannel());
System.out.println(b.getFD());
}
catch(Exception e)
{
System.out.println(e);
}
}
}

here i am getting below Output which is correct
Code:
[root@1s home]# cat k
Aeiou[root@1s home]#

but i want output as below. how should i get this. suggest the way forward.
Code:
[root@1s home]# cat k
Aeiou
[root@1s home]#

# 2  
Old 05-25-2018
Hint: Your variable names made it hard for me to read your code. Variable names that indicate what they are -- really help. a,b,c,d,e do not convey much.

Code:
b.write(c,1,4);

This creates what you see in the file. "\n" works to solve your problem - watch out for windows OS.

So you need another call to write to c with one character, "\n".

Here is a lot more advanced discussion - note that java versions have different limitation on the use of writing to numbered file descriptors - depends on version and OS.
Using a numbered file descriptor from Java - Stack Overflow
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Programming

Not getting O/P in expected way-java

through below code i am trying to write a content in a file and then reading the same file my code is running file but is not getting in proper way import java.io.*; class A1 { public static void main (String agrs) { byte b = {'a','e','i','o','u'}; String s = "King Maker"; try {... (7 Replies)
Discussion started by: scriptor
7 Replies
Login or Register to Ask a Question