Question

In: Computer Science

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:

  1. UTF8ToUTF16.java
  2. UTF16ToUTF8.java
  3. A sample utf8 input file
  4. A sample utf 16 output file
  5. A sample utf 16 input file
  6. A sample utf 8 output file
  7. A screenshot of your computer running program 1
  8. A screenshot of your computer running program 2

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

Solutions

Expert Solution

WriteUTF16.java

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

WriteUTF8.java

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

UTF16 Output

UTF8 Output

Running Program


Related Solutions

Write a java program that will ask for a Star Date and then convert it into...
Write a java program that will ask for a Star Date and then convert it into the corresponding Calendar date. without using array and methods.
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Write a java program to convert a positive integer value to a roman number. Your program...
Write a java program to convert a positive integer value to a roman number. Your program should print a description, prompt them for how many numbers they need to convert and then loop that many times. Each time the program needs to prompt for a number and output the equivalent roman numeral. If the user enters a number less than 1 then output an error message. See sample output below: **************************************************** * Welcome to the Roman Numeral Converter!          * *...
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...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in LiveExample 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
Please show all work an explain to be upvoted. Write an assembly program to convert an...
Please show all work an explain to be upvoted. Write an assembly program to convert an 8 character string from upper case to lower case.
IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token...
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token = nextToken() while (token != "#") if (token is an operand) append token to postfix expression else if (token is "(") // Left paren - Start of sub-expr OpStk.push( token ) else if (token is ")") // Right paren - End of sub-expr pop operators off the stack and append to the postfix expression - stop when you've popped a "(" else (token is...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit to degrees Celsius. The program should prompt for a temperature in Fahrenheit and output a temperature in Celsius. All calculations should be done in in ints, so be careful of truncation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT