简体   繁体   English

我想知道如何将其写入文件

[英]i was wondering how i would go about writing this to a file

How do i add an array in a file menu, the code below lets me input 10 artists and 10 songs for each artist. 如何在文件菜单中添加数组,下面的代码让我输入10位歌手和10位歌手。

import java.io.*;
public class Music {

    public static void main(String[] args) throws IOException
    {
        BufferedReader stuff = new BufferedReader(new InputStreamReader(System.in));
        String inData;
        int [] [] MUSIC = new int [2] [10];
        for (int counter = 0; counter <= 1; counter++)
        {
            System.out.print( "Enter Artist: ");
            inData = stuff.readLine();

            for (int index = 0; index<=9 ;index++)
            {
                System.out.print( "Enter Record: ");
                inData = stuff.readLine();

            }
        }
        System.out.println( "Below is a printout");

        for (int counter = 0; counter <= 20; counter++)
        {
            System.out.print("The nos on Row "+ counter + " are ");
            for (int index = 0; index<= 20;index++)
            {

            }
            System.out.println();
        }
    }
}

I believe that i need to put something like case 1 here and maybe have the code to write the file in a class above the main. 我相信我需要在这里放一个类似情况1的东西,也许要有代码在main之上的类中写入文件。

The simplest way is to redirect System.out to a file on the command line. 最简单的方法是将System.out重定向到命令行上的文件。 Alternatively you could reassign System.out in your code or better still do some file I/O - all of these have been discussed here System.out to a file in java 或者,您可以在代码中重新分配System.out或更好地执行一些文件I / O-所有这些都已在此处讨论过System.out到Java中的文件

You are not really storing the entire artists name in the array. 您实际上并没有将整个演出者姓名存储在数组中。 Here is one way you can do this. 这是您可以执行此操作的一种方法。

  1. Create 2 String Arrays. 创建2个字符串数组。 One for the artist name and the second(multi dimensional) for the tracks. 一个用于艺术家名称,第二个(多维)用于曲目。

  2. For each index, store the fetched artist name in the 1st array and the track in the second array for the same index. 对于每个索引,对于相同的索引,将获取的艺术家名称存储在第一个数组中,将曲目存储在第二个数组中。

  3. Fetch the data from the 2 arrays using the lopp that you have written and write it to a file using one of the different approaches mentioned in this thread 使用已写入的lopp从2个数组中获取数据,并使用此线程中提到的不同方法之一将其写入文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM