Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to read local binaries into String strings

Shulou Source: shulou.com Published: 2022-06-02 02:40:32 10月03日 Update

This article shows you how to read local binaries into the String string, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Note: this utility class can only read local files.

Import java.io.*;/** * File manipulation tool Class * * @ author edison_kwok * / public class FileUtils {/ * Local File renaming * * @ param file * @ param newName * / public static void rename (File file, String newName) {if (file.exists ()) {String absolutePath = file.getAbsolutePath () String path = absolutePath.substring (0, absolutePath.lastIndexOf ("\") + 1); file.renameTo (new File (path + newName)) }} / * function: Java reads the contents of the txt file * step: 1: get the file handle * 2: get the file handle as an input byte stream, and you need to read the input stream * 3: after reading the input stream, you need to read the output of the generated byte stream * 4: one line. Readline (). * Note: the exception needs to be considered * * @ param filePath * / public static String readFile (String filePath) {InputStreamReader read = null; BufferedReader bufferedReader = null; String lineTxt = null; try {String encoding = "UTF-8"; File file = new File (filePath) / / determine whether the file exists if (file.isFile () & & file.exists ()) {/ / considering the encoding format read = new InputStreamReader (new FileInputStream (file), encoding); bufferedReader = new BufferedReader (read); StringBuilder stringBuilder = new StringBuilder () While ((lineTxt = bufferedReader.readLine ())! = null) {stringBuilder.append (lineTxt);} return stringBuilder.toString ();} else {throw new RuntimeException ("the file does not exist") } catch (Exception e) {throw new RuntimeException (e.getMessage ());} finally {try {if (bufferedReader! = null) {bufferedReader.close ();} if (read! = null) {read.close () }} catch (IOException e) {e.printStackTrace ();}} / * the output stream exports the local file * * @ param is * @ param os * @ throws Exception * / public static void fileUpload (InputStream is, OutputStream os) throws Exception {byte [] b = new byte [2048] Int length = 0; while (true) {length = is.read (b); if (length < 0) break; os.write (b, 0, length);} is.close (); os.close ();}} the above is how to read local binaries into the String string. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: File content input binary character string line handle that is tools skills knowledge output concise concise functions comments bytes situations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MariaDB Apple OPPO Reno Docker