Not getting O/P in expected way-java


 
Thread Tools Search this Thread
Top Forums Programming Not getting O/P in expected way-java
# 1  
Old 05-29-2018
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
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);
}
}
}

i am getting output as below

Code:
 
 [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.

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

# 2  
Old 05-29-2018
println() means "print line", it prints lines.

You could assemble the entire line you wanted to print before printing it.
These 3 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 05-30-2018
hi Corona
thx a lot for help getting o/p as expected
Code:
[root@1s home]# java A1
AaeiouKing Makerhappy

.


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

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


I do not want this part
Code:
"[root@1s home]#"

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

please suggest
# 4  
Old 05-30-2018
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.
# 5  
Old 05-30-2018
HI Rudic,

below is the code
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);
}
}
}

# 6  
Old 05-30-2018
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.
# 7  
Old 05-30-2018
HI Rudic,

can you guide me where to add new line and how
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Programming

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 vi A.java import java.io.FileOutputStream; class A { public static void main (String args) { byte c= {'a','e','i','o','u'}; try {... (1 Reply)
Discussion started by: scriptor
1 Replies
Login or Register to Ask a Question