Question

In: Computer Science

Write two Java programs and verify that they work. Program 1 should: Read a UTF-8 encoded...

Write two Java programs and verify that they work.

Program 1 should:

  • Read a UTF-8 encoded text file
  • Print to the console the hex values for the bytes it read in.
  • Convert the text to UTF-16
  • Print to the console the hex values for the converted bytes
  • Write the UTF-16 text to a file

Program 2 should do the same as program 1, except program 2 goes from UTF-16 to UTF-8

Note: we are supposed to write two java programs, the first program should read utf8 encoded text file and the second program should write utf16 text file. So, if you can give me the java code that can read and write utf8 and utf16 text files respectively , I would be happy. Thank you.

Solutions

Expert Solution

//Program 1

import java.io.*;

public class WriteUTF8 {
  
   public static void main(String[] args) {
       File file = new File("utf8_output.txt");
       FileOutputStream fs= null;
       try {
           fs = new FileOutputStream(file);
       }
       catch (FileNotFoundException fnfe) {
           System.err.println("file not found");
       }
       try {
           fs.write("Фёдор".getBytes("UTF-8"));
           fs.close();
       }
       catch(IOException ioe){
           System.err.println("unable to write to file");
       }
   }
}
---

import java.io.*;

public class WriteUTF16 {
  
   public static void main(String[] args) {
       File file = new File("utf16_output.txt");
       FileOutputStream fs= null;
       try {
           fs = new FileOutputStream(file);
       }
       catch (FileNotFoundException fnfe) {
           System.err.println("file not found");
       }
       try {
           fs.write("Фёдор".getBytes("UTF-16"));
           fs.close();
       }
       catch(IOException ioe){
           System.err.println("unable to write to file");
       }
   }
}
--
//Program 2
import java.nio.charset.Charset;

public class StringLength{
   public static void printBytes(byte[] byteArr){
       System.out.print("Bytes ( in hex ): ");
       for ( byte b : byteArr ){
           System.out.print(String.format("%02X ", b));
       }
       System.out.println("");
   }
  
   public static void inspectBytesOfText(String data, String textEncoding) {
       System.out.println("\n\n**********************************");
       System.out.println( "************** " + textEncoding + " ************\n");
      
       System.out.println("Length of string " + data + " = " + String.valueOf(data.length()));
       byte[] dataBytes = data.getBytes(Charset.forName(textEncoding));
       System.out.println("Number of bytes in " + data + " = " + dataBytes.length);
      printBytes(dataBytes);

   }
  
   public static void main(String[] args){
       inspectBytesOfText("Фёдор", "UTF-8");
       inspectBytesOfText("Фёдор", "UTF-16");
      
       inspectBytesOfText("hello", "UTF-8");
       inspectBytesOfText("hello", "UTF-16");

       // Not that eclipse ( at least on my computer at 10:49 AM of 9/29/2020 )
       // does not perfectly support unicode strings.
       // When I type an emoji with a halo in, the following quotation mark is
       //displayed in a weird way.
       inspectBytesOfText("😇", "UTF-8");
       inspectBytesOfText("😇", "UTF-16");
      
       // if you know a foreign language, try running this method with a string from that language/
       // try using emojis too.
   }
}

Related Solutions

Convert UTF-8 to UTF-16 and Back Write two Java programs and verify that they work. Program...
Convert UTF-8 to UTF-16 and Back Write two Java programs and verify that they work. Program 1 should: Read a UTF-8 encoded text file Print to the console the hex values for the bytes it read in. Convert the text to UTF-16 Print to the console the hex values for the converted bytes Write the UTF-16 text to a file Program 2 should do the same as program 1, except program 2 goes from UTF-16 to UTF-8 Submit: UTF8ToUTF16.java UTF16ToUTF8.java...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n Multiplying that matrix to the nth power. Like A^2 = matrix A * matrix A. Elements ONLY can be 0 or 1.
In Java: "The program should be able to read a two-digit number d from the standard...
In Java: "The program should be able to read a two-digit number d from the standard input using Scanner class and outputs the second digit followed by a string literal "<-->" followed by the first digit. Run your program and make sure it prints 2<-->5 when d=52"
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
For each problem below, write a java program to solve it, name your programs as PA2_1.java...
For each problem below, write a java program to solve it, name your programs as PA2_1.java and PA2_2.java. 1. We have a steam heating boiler whose bursting pressure is known, but we of course want to use it only at pressures well below this limit. Our engineers recommend a safety factor of 3; that is, we should never exceed a pressure that is one-third of the rated bursting pressure. Write a java program that reads in the rated bursting pressure...
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT