Hallo zusammen
Also folgendes ist der Auftrag:
Nun habe ich bereits folgendes überlegt und programmiert:
und
und die Main Klasse:
Jetzt wird aber immer null ausgegeben und der Puffer funktioniert nicht so wie er sollte. Könnt ihr mir diverse Anpassungsmöglichkeiten geben oder auch Tipps was ich verändern könnte?
Danke!
Also folgendes ist der Auftrag:
- Cache für Zugriff auf Dateien implementieren und Performance Evaluationen durchführen
- Puffer soll Zeilenweise Inhalte einer Datei zwischenspeichern
- Es sollte möglich sein die Grosse des Puffers in Anzahl Zeilen zuspezifizieren
- Beim Initialisieren liest der Puffer den Inhalt der Datei (Grossenbeschränkung beachten) Puffer
- ■ Open(Filename)
- Wird eine Zeile verlangt, die nicht im Puffer vorhanden ist, muss diese gelesen werden. Wenn kein Platz frei ist, muss eine Zeile vom Puffer entfernt werden
- Testen Sie Ihre Implementierung mit und ohne Puffer und stellen Sie die Resultate grafisch dar
- Versuchen Sie eine standardisierte Schnittstelle zu implementieren.
Nun habe ich bereits folgendes überlegt und programmiert:
Java:
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class SimpleCacheManager {
private static SimpleCacheManager instance;
private static Object monitor = new Object();
private Map<String, ObjectCache> cache = Collections
.synchronizedMap(new HashMap<String, ObjectCache>());
private SimpleCacheManager() {
}
public void put(String cacheKey, ObjectCache value) {
cache.put(cacheKey, value);
}
public Object get(String cacheKey) {
return cache.get(cacheKey);
}
public void clear(String cacheKey) {
cache.put(cacheKey, null);
}
public void clear() {
cache.clear();
}
public static SimpleCacheManager getInstance() {
if (instance == null) {
synchronized (monitor) {
if (instance == null) {
instance = new SimpleCacheManager();
}
}
}
return instance;
}
/**
* Retrieves a file from a cache. Puts it into the cache if it's not cached.
*
* @param pathName a file path name.
* @return a cached file content or null if file not found
* @throws IOException if an I/O error occurred.
*/
public String getFileFromCache(String pathName) throws IOException {
// Check if file exists
File file = new File(pathName);
if (!file.exists()) {
// Invalidate cache
cache.remove(pathName);
// Return null (not found)
return null;
}
// Get the file from the cache
ObjectCache objectCache = cache.get(pathName);
// Check if the cached file exists
if (objectCache == null) {
// Not found in the cache, put in the cache
objectCache = readFile(file);
cache.put(pathName, objectCache);
} else {
// Found in cache, check the modification time stamp
if (objectCache.getLastModified() != file.lastModified()) {
// Update cache
objectCache = readFile(file);
cache.put(pathName, objectCache);
}
}
return objectCache.getContent();
}
/**
* Reads a file into a new TextFile object.
*
* @param file the file to read from.
* @return a new TextFile object.
* @throws IOException if an I/O error occurred.
*/
private ObjectCache readFile(File file) throws IOException {
// Read the file content into a StringBuilder
char[] buffer = new char[1000];
FileReader fileReader = new FileReader(file);
StringBuilder fileContent = new StringBuilder((int) file.length());
for (int bytesRead = fileReader.read(buffer); bytesRead != -1; ) {
fileContent.append(buffer, 0, bytesRead);
}
// Close the reader
fileReader.close();
// Create CachedTextFile object
ObjectCache objectCache = new ObjectCache();
objectCache.setContent(fileContent.toString());
objectCache.setLastModified(file.lastModified());
// Return the result
return objectCache;
}
}
und
Java:
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* A value object containing a cached file and file attributes.
*/
public final class ObjectCache implements Externalizable {
private long lastModified;
private String content;
public ObjectCache() {
}
public long getLastModified() {
return lastModified;
}
public String getContent() {
return content;
}
public void setContent(final String content) {
this.content = content;
}
public void setLastModified(final long lastModified) {
this.lastModified = lastModified;
}
public void writeExternal(final ObjectOutput out) throws IOException {
out.writeLong(lastModified);
out.writeUTF(content);
}
public void readExternal(final ObjectInput in) throws IOException {
lastModified = in.readLong();
content = in.readUTF();
}
public String toString() {
return "TextFile{" +
"lastModified=" + lastModified +
", content='" + content + '\'' +
'}';
}
}
und die Main Klasse:
Java:
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
// Set the path
String pathName = "C:\temp\testfile.txt";
SimpleCacheManager scm = SimpleCacheManager.getInstance();
String fileFromCache = scm.getFileFromCache(pathName);
System.out.print("File content from cache: " + fileFromCache);
}
}
Jetzt wird aber immer null ausgegeben und der Puffer funktioniert nicht so wie er sollte. Könnt ihr mir diverse Anpassungsmöglichkeiten geben oder auch Tipps was ich verändern könnte?
Danke!