Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In this program I want to print the length of string in a external file but the problem is that it make the number of character in a string as a decimal value of character and prints the character which matches with the ascii value and print in a file.

import java.io.FileOutputStream;
        class Fileoutputstream
        {
            public static void main(String args[])
            {
                try
                {
                    FileOutputStream foul=new FileOutputStream("/root/Documents/YG2108/DemoFiles.txt");
                    String s="hello Sir ";
                    String s1;
                    byte by[]=s.getBytes();
                    foul.write(by);
                    s1="Good Afternoon have a nice day frghunv9uhbzsmk zvidzknmbnuf ofbdbmkxm;jccipx nc     xdibnbnokcm knui9xkbmkl bv";
                    by=s.getBytes();
                    int yb=s.length();
                    int ascii = yb;     
                    foul.write(ascii);

                    System.out.println("Sucess");
                }catch(Exception e){System.out.println(e);}     
            }
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

Assuming you want to write length() of string s1 in DemoFiles.txt

import java.io.FileOutputStream;
    class Fileoutputstream
    {
        public static void main(String args[])
        {
            try
            {

         File file=new File("C:\Users\1201567\Desktop\test.txt");

         BufferedWriter bf=new BufferedWriter(new FileWriter(file));

         String s1="Good Afternoon have a nice day frghunv9uhbzsmk zvidzknmbnuf ofbdbmkxm;jccipx nc     xdibnbnokcm knui9xkbmkl bv";

         int length=s1.length();

         bf.write(String.valueOf(length));
         bf.flush();
         System.out.println("Sucess");
     }catch(Exception e){System.out.println(e);}     
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...