There is an int[] array, it contains such integers: 18 3 34 35 8 0 22 10 , how to write this into a .txt file and count to get not 1 8 3 3 4 3 5 8 , etc. or ASCI codes, and the original line?
Thank!

    3 answers 3

     import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class FileReadWriteExample { static final File FILE = new File("tester/hello.txt"); static Integer[] array = {18, 3, 34, 35, 8, 0, 22, 10}; public static void main(String[] args) throws IOException { writeArray(array, FILE); Integer[] array = readArray(FILE); System.out.println(Arrays.asList(array)); } static void writeArray(Integer[] array, File file) throws FileNotFoundException{ PrintStream printStream = new PrintStream(file); for(Integer i : array) { printStream.print(i); printStream.append(' '); } } static Integer[] readArray(File file) throws FileNotFoundException{ Scanner scanner = new Scanner(file); List<Integer> list = new ArrayList<>(); while (scanner.hasNext()){ list.add(scanner.nextInt()); } Integer[] array = new Integer[list.size()]; list.toArray(array); return array; } } 

      The easiest way:

        import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.logging.Logger; public class ArrayInt { private static final Logger LOG = Logger.getLogger(ArrayInt.class.getName()); public static void main(String[] args) { int[] a = {18, 3, 34, 35, 8, 0, 22, 10}; try { File file = File.createTempFile("www", ".txt"); System.out.println(file.getAbsolutePath()); try (FileWriter fileWriter = new FileWriter(file);) { for (int i : a) { fileWriter.write(i); } fileWriter.flush(); } List<Integer> listInt = new ArrayList(); try (FileReader fileReader = new FileReader(file)) { int ch; while ((ch = fileReader.read()) != -1) { listInt.add(ch); } Integer[] newA = new Integer[listInt.size()]; newA = listInt.toArray(newA); System.out.println(Arrays.toString(newA)); } } catch (IOException e) { e.printStackTrace(); } } } 

        Using serialization (even without libraries) can be implemented like this:

        1. typed

           public static void main(String[] args) throws Exception { int mass[] = {8, 16, 32}; ByteBuffer buffer = ByteBuffer.allocate(mass.length * 4); buffer.asIntBuffer().put(mass); Files.write(Paths.get(tmp.txt), buffer.array(), StandardOpenOpetion.CREATE, StandardOpenOption.WRITE); int massRead[] = new int[mass.length]; ByteBuffer.wrap(Files.readAllBytes(Paths.get("tmp.txt"))).asIntBuffer().get(massRead); System.out.println(Arrays.equals(mass, massRead)); } 
          1. without typing

            public static void main (String [] args) throws Exception {int mass [] = {8, 16, 32};

             // добавить закрытие стримов new ObjectOutputStream(new FileOutputStream("tmp.txt")).writeObject(mass); 

            int massRead [] = (int []) (new ObjectInputStream (new FileInputStream ("tmp.txt")). readObject ());

             System.out.println(Arrays.equals(mass, massRead)); 

            }

        PS there is no development environment at hand, wrote in a notebook, correct if something goes wrong