Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

java - How to print the length of a string to the file

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

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

1 Reply

0 votes
by (71.8m points)

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);}     
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...