简体   繁体   中英

Calling a constructor of a class to modify it to use in another class

The purpose of this program is to read and write files to a text file.

For this i have three classes:

Class ReadFile //Reads and displays text from the text file

Class WriteFile //Gets input from user and puts it writes it to the text file 

Class Application //Holds the Main method for execution.

In the WriteFile class I have code to either append (add) text to the text file or not append (delete everything in the text file then write input to the text file which is accomplished by this code here

public class WriteFile 
{

private String path;
private boolean append_to_file = false;

public WriteFile(String file_path)
{
    path = file_path;
}

public WriteFile (String file_path, boolean append_value)
{
    path = file_path;
    append_to_file = append_value;
}

public void writeToFile(String textLine) throws IOException
{
    FileWriter write = new FileWriter(path);
    PrintWriter print_line = new PrintWriter(write);

    print_line.printf("%s" + "%n", textLine);
    print_line.close();
}
}

Now the only way to decide wether or not to append data to the text file is to comment out

append_to_file = append_value;

which of course the user will not be able to do. I would like to give the user this option.

So i thought I could add this in Class Application which holds the code to recieve inputs.

public class Application 
{

public static void main(String[] args) throws IOException
{



String file_name = "Q:/test.txt";

try
{

    ReadFile file = new ReadFile(file_name);
    String[] aryLines = file.OpenFile();

    for(int i = 0; i < aryLines.length; i++)
    {
        System.out.println(aryLines[i]);    
    }
}

catch (IOException e)
{
    System.out.println( e.getMessage());
}

//boolean End = false;
String userEnter;
Scanner input = new Scanner (System.in);

for(int i = 0; i < 10; i++)
{
System.out.println("\n\nenter text to write to file");
userEnter = input.nextLine();

WriteFile data = new WriteFile(file_name, true);
data.writeToFile( userEnter );

System.out.println( "Text File Written To" );
}

}
}

How can I write code to give the user the option to append data?

Here is Class ReadFile if it helps

public class ReadFile 
{
private String path;

public ReadFile (String file_path)
{
    path = file_path;
}

public String[] OpenFile() throws IOException
{
    FileReader fr = new FileReader(path);
    BufferedReader textReader = new BufferedReader(fr);

    int numberOfLines = readLines();
    String[] textData = new String [numberOfLines];



    for(int i = 0; i < numberOfLines; i++)
    {
        textData[i] = textReader.readLine();
    }

    textReader.close();
    return textData;
}

int readLines() throws IOException
{
    FileReader file_to_read = new FileReader(path);
    BufferedReader bf = new BufferedReader(file_to_read);

    String aLine;
    int numberOfLines = 0;

    while ((aLine = bf.readLine()) != null)
    {
        numberOfLines++;
    }
    bf.close();
    return numberOfLines;
}

}

I hope i explained this clearly

Thankyou Keil

If you want the user to always append, or always overwrite for a specific set of lines, you can do the following:

System.out.println("Enter false to overwrite data, or true to append data");
userEnter = input.nextLine();
boolean append = Boolean.parseBoolean(userEnter);

if you put those lines outside of your loop, the user will get asked once, and the same value will always be used, or you can put it inside your loop to ask each time.

WriteFile data = new WriteFile(file_name, append);

You can easily use the Scanner to ask the user for Y/N input. Convert this input to a boolean and pass it to your WriteFile constructor.

So if im understanding you want the user to be able to select if append to file or overwrite it.

Then you can do something like this

String userEnter;
Scanner input = new Scanner (System.in);

for(int i = 0; i < 10; i++)
{
boolean append = true;
System.out.println("Want to overwrite file? (Y/N)");
if(input.nextLine().compareTo("Y")==0)
append=false

System.out.println("\n\nenter text to write to file");
userEnter = input.nextLine();

WriteFile data = new WriteFile(file_name, append);
data.writeToFile( userEnter );

System.out.println( "Text File Written To" );
}

Now if user writes Y on overwrite question append_to_file will be false otherwise it will be true.

This is really basic Java, i hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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