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
{
FileOutputStream f = new FileOutputStream("/home/tes1");
FileInputStream fis = new FileInputStream("/home/tes1");
byte p[]=s.getBytes();
f.write(65);
f.write(b);
f.write(p);
f.close();
int v ;
while(( v = fis.read())!=-1)
{
System.out.println((char)v);
}
fis.close();
System.out.println("happy");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
 

i am getting output as below

 
 [root@1s home]# java A1
A
a
e
i
o
u
K
i
n
g
 M
a
k
e
r
happy
 

however i want output as below.

 
 [root@1s home]#Aaeiou King Maker happy
 

println() means "print line", it prints lines.

You could assemble the entire line you wanted to print before printing it.

3 Likes

hi Corona
thx a lot for help getting o/p as expected

[root@1s home]# java A1
AaeiouKing Makerhappy

.

however when I open the file which I created is showing O/P as below

[root@1s home]#cat tes1
AaeiouKing Maker[root@1s home]#

I do not want this part

"[root@1s home]#"

to come after file content.
how should I get rid from it .

please suggest

The final <new line> is obviously missing, "this part" is the shell's prompt for the next command. How did you print your output? Please show your adapted code.

HI Rudic,

below is the code

 
 import java.io.*;
class A1
{
public static void main (String agrs[])
{
byte[] b = {'a','e','i','o','u'};
String s = "King Maker";
try
{
FileOutputStream f = new FileOutputStream("/home/tes1");
FileInputStream fis = new FileInputStream("/home/tes1");
byte p[]=s.getBytes();
f.write(65);
f.write(b);
f.write(p);
f.close();
int v ;
while(( v = fis.read())!=-1)
{
System.out.println((char)v);
}
fis.close();
System.out.println("happy");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
 

You're doing two things here: you are writing several constants and variables to a temp file, without a <new line> character, and close that file. Then, you read that file, print it to screen / terminal including a <new line>, and add another string constant with <new line>.

No surprise the <new line> is missing when you cat the temp file to screen.

HI Rudic,

can you guide me where to add new line and how

Now, I'm not at all familiar with java , so please take my advice cum grano salis. Try adding a f.write(10); before closing the file.