This site requires JavaScript, please enable it in your browser!
Greenfoot back
GreenAnthony
GreenAnthony wrote ...

2014/1/30

Changing the name of the folder every time

GreenAnthony GreenAnthony

2014/1/30

#
Okay following problem: Some people on here finally thought me how to create files but now I got a small problem. I have to create very many files and I don't to copy and paste the code over and over so I wanted to know if there is a way of changing the file name after each time it has been created. Here is the code that creates my files:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
BufferedWriter file = null;
try
                file = new BufferedWriter(new FileWriter("Filename.txt")); 
                file.write("Here is where the text would be"); 
                file.close(); 
            
            catch (IOException ioe) { 
                ioe.printStackTrace(); 
            
            finally
                try
                    file.close(); 
                
                catch (IOException ioe) { 
                    ioe.printStackTrace(); 
                
            }
Its okay that the files always have the same writing in them but I need to figure out how to change the "Filename.txt" part after it has been created otherwise it would always overwrite itself instead of making new files. Is that possible? Can somebody tell me how?
bourne bourne

2014/1/30

#
The easiest way is having a variable filename in the code, which is set to some input. Or you could check for existing Files in the directory, which if you give me a minute I could come up with something.
bourne bourne

2014/1/30

#
The code below will make sure not to overwrite a previous file. For example, if there were files with names "Filename9.txt" and "Filename4.txt", the new File would have name "Filename10.txt"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
String filePrefix = "Filename";
String fileExtension = ".txt";
int fileNumber = -1;
 
File[] files = new File("/Users/taylor/Documents/Programming things/Greenfoot/Testing").listFiles();///path/to/the/directory
 
for (File file : files)
    if (file.isFile()) {
        String fileName = file.getName();
        if (fileName.startsWith(filePrefix) && fileName.endsWith(fileExtension))
        {
            int i;
            try {
                String s = fileName.substring(filePrefix.length(), fileName.length() - fileExtension.length());
                i = Integer.parseInt(s);
            } catch (Exception e) {
                continue;
            }
            if (i > fileNumber)
                fileNumber = i;
        }
    }
BufferedWriter file = null;
try {
    file = new BufferedWriter(new FileWriter(filePrefix + (fileNumber + 1) + fileExtension));   
    file.write("Here is where the text would be");
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {   
        file.close();   
    } catch (Exception e) {}
}
Note that since the finally block is always executed, you don't need your first file.close() from your given code
danpost danpost

2014/1/31

#
If it does not matter that the files (plural) are over-wrote during different runs of the scenario, only that you can create a number of different files during each run, then you could use an int field (call it 'fileNum') and use this for your line 3:
1
file = new BufferedWriter(new FileWriter("Filename"+(fileNum++)+".txt"));
The 'fileNum' field can also serve to track the number of files written for that running of the scenario. EDIT: I guess this is similar to the suggestion bourne gave; the only differences being that his will always write to a file using the next available filenumber above any other and does not have a way to indicate which files were written during that particular running of the scenario (however, a List object could be used for that purpose). It would depend on what you needed as to which way you go here.
GreenAnthony GreenAnthony

2014/1/31

#
Ok I decided to use yours danpost (just because its shorter) but it tells me it can not find the variable file. Did I miss something?
GreenAnthony GreenAnthony

2014/1/31

#
Nearly the same problem with bourne... Sorry its just that I am new to writing files and stuff... bourne this is what I wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
File[] files = new File("/Users/Anthony's MacBook Air/Destop").listFiles();///path/to/the/directory 
        for (File file : files) 
            if (file.isFile()) { 
                String fileName = file.getName(); 
                if (fileName.startsWith(filePrefix) && fileName.endsWith(fileExtension)) 
                
                    int i; 
                    try
                        String s = fileName.substring(filePrefix.length(), fileName.length() - fileExtension.length()); 
                        i = Integer.parseInt(s); 
                    } catch (Exception e) { 
                        continue
                    
                    if (i > fileNumber) 
                        fileNumber = i; 
                
        
        BufferedWriter file = null
        try
            file = new BufferedWriter(new FileWriter(filePrefix + (fileNumber + 1) + fileExtension));     
            file.write("Here is where the text would be"); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } finally
            try {     
                file.close();     
            } catch (Exception e) {} 
        }
Of course I also did the int things... But can you found what I did wrong?
davmac davmac

2014/1/31

#
1
File[] files = new File("/Users/Anthony's MacBook Air/Destop").listFiles();
Looks like you misspelled 'Desktop' in the file path.
GreenAnthony GreenAnthony

2014/2/1

#
dacmac thanks for telling me the small embarrassing mistake i made but it still doesn't work it says: cannot find symbol - class File for this line:
1
File[] files = new File("/Users/Anthony's MacBook Air/Desktop").listFiles();
What should I do?
danpost danpost

2014/2/1

#
Are you importing the File class or 'io' package with something like the following?
1
import java.io.*;
GreenAnthony GreenAnthony

2014/2/1

#
well I got those:
1
2
3
import java.io.FileWriter; 
import java.io.BufferedWriter; 
import java.io.IOException;
But good point I will try it again!
GreenAnthony GreenAnthony

2014/2/1

#
still doesn't work....
danpost danpost

2014/2/1

#
GreenAnthony wrote...
well I got those:
1
2
3
import java.io.FileWriter; 
import java.io.BufferedWriter; 
import java.io.IOException;
But good point I will try it again!
Remove those and just use:
1
import java.io.*;
Then compile and try it.
GreenAnthony GreenAnthony

2014/2/1

#
Okay i found out myself I changed my original code a little and now it works... but still thank all of you for trying and giving me codes!
You need to login to post a reply.